48 points · by steinsgate · 10y ago · 82 comments
What's a type hint for if, after that, the user can actually pass a different type?
Statically typed languages (1) introduce limitations so that type checking is decidable (no incertitude), or (2) or treat "don't know" as "reject", because the runtime has practically no knowledge of type. This is not the case in Python, so you don't have to check everything. That does not mean you shouldn't check anything.
So there are still benefits in Python to have type in order to "reject" code that is undoubtedly going to produce type errors at runtime (or maybe to optimize things). In case of uncertainty, you know the runtime is going to catch errors anyway (including type errors).
Type hinting would be ok for documentation only. But then, a comment is fine and introduces less clutter.
The warnings that are emitted by SBCL (Lisp implementation) are produced when two types don't intersect at all, something like "len(3)". There is no warning when types overlap. And even if the warning is issued, the code is still compiled and can run.
But I'm inclined to agree. It's a little too to add static typing to Python. You're just going to end up with something far worse than a language that was designed with it in mind, like Ceylon or Go.
Which statically typed scripting language would you suggest instead of Python?
Would you like something that you can embed? Something that has an interpreter for?
https://en.wikipedia.org/wiki/Scripting_language
> Would you like something that you can embed? Something that has an interpreter for?
I asked about your personal preference regarding a statically typed alternative to Python. Can you propose one?
Extremely frustrated by this decision and even though the multiple denials that it won't become a statically typed language it's clear that idea has changed
And "optional" is a misnomer unless you only ever touch your own code. Programming is generally a social activity, so for most of us, "optional" means just means "inconsistently required".
I don't have a philosophical objection to typing, but I'm yet to see a syntax that won't muddy Python's.
Optionally means that it's not required or mandatory.
You don't need to use it if you don't want to. If you're working on a project and the project follows a style guide then you follow the style guide, but nothing forces your bosses to require it. The code will run anyway.
People will start using and it will become mandatory at some point
"Dynamically typed Python variables considered harmful"
It seems it's time to abandon Erlang since static typing fundamentalists seem to be getting hold of it, with its typespecs and Dialyzer.
Oh wait, it's still dynamically typed.
And I'm all for Pylint, it also proves type hints are not needed for this kind of type analysis
No typespecs feels like crippling the tool, except it was designed to be as useful as possible for old code without modifying it. It just shows how useful the typespecs are.
1) I have the ability to define a type that's required so that the function can't be called without a numeric type.
2) I add boiler plate the the top of my function to validate that I'm receiving a number.
3) I forget to do 1 or 2 and I get a runtime error when a different developer inadvertently passes an incorrect type to my function.
Most heavily used libraries in most dynamic languages, not only Python, ought to have assertions with type checks for parameters. In the external API calls to the lib.
Inside of a lib you should have tests anyway, so type declarations shouldn't be important.
The beauty of python was that it could read like pseudocode; more and more special characters and notations are putting that idea to rest. I guess it's the price to pay for mainstream success -- when banks get involved, everything becomes uber-formal and soul-crushing.
And this is bad because...?
You want Java you use Java. Oh it sucks? I wonder why...
Oh even better, you set a type, does it prevent you from using things with a similar interface? Like iterables in place of lists?
Better code documentation? Static analysis? Compare to Erlang with typespecs and Dialyzer.
> You want Java you use Java.
Why Java? Why not SML, OCaml, Haskell, Rust? Those are statically typed languages, too.
> Oh it [Java] sucks? I wonder why...
Because it's statically typed yet it's dynamically typed ("Object" class thing)? Because its type system is quite primitive and somewhat inconsent? Because it was designed for use in TV sets two dozens years ago, and has barely advanced as a language since then? (No, foreach loops and lambdas don't qualify as a significant step, those are merely syntax sugar.)
> Oh even better, you set a type, does it prevent you from using things with a similar interface? Like iterables in place of lists?
Type specification signifies code author's intent. This is a very good thing, and I want to see it much more in the code. I don't expect type specs/hints to do anything more for a dynamically typed language.
> because they will lose readability and raise the bar for contribution from novices
OK, hold for a second. Adding something to make the code easier to follow will make code harder to follow? Is this really your argument?
And why would a novice to a language want to contribute to "serious codebase"? I don't think Django, Jinja, or NumPy are popular for Python novices to contribute code.
If people really want their bulky IDEs to give them maximum help when introspecting a codebase, there should just be a TypeScript like superset of Python that those people can use.
This syntax is awful and will seriously cause antagonism in terms of what people consider "pythonic" and "good code".
my_class_attr = ClassAttr[bool]: True
This seems more familiar somehow.Also, that style is only more familiar if you're coming from C, C++, Java, &c. However, the style in the PEP is pretty common in languages outside of the C family, such as those inspired by Pascal, and there's at least two examples of C family languages - Go and Swift - which use the style in the PEP.
Also, putting the declaration on the LHS likely results in fewer grammar ambiguities, but it's well over a decade since I last looked at Python's grammar, so that's just speculation on my part. I know that's one of the reasons why Swift and Go went down the route they did, and Python would have even more reason, given how ':' is used in the language.
If this feature came at relatively little cost this would be great - 5% of bugs squashed just like that. It doesn't. It contributes significantly to the verbosity of the code - chipping away at one of python's main strengths - concision and clarity.
No, it doesn't. The only places this is meant to be used is at interface points, i.e., declaring object member attributes and method/function signatures. We're not talking Java or C++ levels of type annotation here: we're talking Go, Haskell, and O'Caml levels of type annotation at most.
And this is the core of the FUD around type annotations: people act like it's introducing Java and C++ levels of needless crud into the language, when the actual number of places where it's meant to be used is much smaller.
Also, while it works almost miraculously well as-is, type annotations would make the likes of Jedi much better. It might not seem like a big thing, but autocompletion is really useful with larger codebases and libraries you don't necessarily use every day.
There's very little extra cost with this over and above what's currently there. All it's doing is formalising something people have been doing in docstrings for decades already.
Some projects are more than eager to cut up to 8% of potential bugs with such a trivial change.
>If this feature came at relatively little cost this would be great - 5% of bugs squashed just like that. It doesn't. It contributes significantly to the verbosity of the code - chipping away at one of python's main strengths - concision and clarity.
I'm not sure you're being serious. Stating a type in a variable declaration doesn't harm concision, and obviously it does exactly the opposite of what you claim regarding clarity.
Furthermore, the most common type errors I do see would be solved more effectively by stricter runtime error checking than they would be by slathering the language in static typing.
For example:
if not x:
do_something()
This example of perly weak runtime type checking is probably responsible for a larger proportion of bugs I see in production than most issues that static typing would solve.Ironically just a month ago a junior changed some of my code from :
if x is not None:
do_something()
To: if not x:
do_something()
Because it was "more pythonic".Not long after he did that he hit a bug because it wasn't supposed to do_something() if x was a blank string. Whoops.
> I've worked on multiple large projects, and while type
> errors are a problem, they aren't in the top 5 classes
> of 'most commonly reported bugs in production' - not
> if you have tests and a sprinkling of sanity checks, anyhow.
My claim is that Pythonists are completely unaware that almost all of the errors they experience could have been avoided by just informing the compiler about what type a value is. I find it peculiar that people prefer writing tests to verify type correctness (a lot of work), rather than just having the compiler check it through simple type annotations. I say this coming from Python, having moved to to Haskell. Two diametrically opposite worlds in terms of type strictness.I think sprinkling type annotations all over the code is much preferable to writing test code. We have a type system such that the compiler can produce the code needed to check types, rather than us having to write this code in a test.
In my humble opinion, humans shouldn't have to write test code. If you can't get the compiler to generate the test code you want - by constructing the types carefully - you're wasting a lot of time, because you have to write both the implementation, and a second implementation which tests the first one. So every time you do a rewrite, you also have to rewrite your tests, so that the two match. Double work.