News for hackers. Stuff that matters.
Re:Python’s “type hints” are a bit of a disappointment to me by civilized on Thursday April 21, 2022 @05:41PM (#31115531)
> not-very-educated thoughts on gradual typing

This seems like an inaccurate and condescending put-down. What is the definition of "educated" in this context? Is there a book or generally well-known resource?

The author is clearly thoughtful and curious. Near the top of the post he says "what I’m hoping for is that someone will come along and tell me that I got it all wrong. “When you do it as follows, the system works: $explanation” Because, you know, I’d really like to have static typing in Python."

> 1. If you're using a dynamic language, then _by definition_ the language will not enforce your static hints at runtime. However, good news! Python has always been strongly typed, and _does_ enforce types at runtime!

And yet, the author points out that this is a working program:

  foo: int = 'hello'
  print(foo)
In what reasonable sense can Python be said to "enforce types at runtime" here?
Re:Python’s “type hints” are a bit of a disappointment to me by ImprobableTruth on Thursday April 21, 2022 @06:24PM (#31115916)
I think the top comment was less polite than it should be, but I would echo that the post seems to have a bit of a weird understanding of gradual typing?

The Any type is to allow incremental typing. You start out with dynamic code, and progressively 'typify' it by adding more and more annotations, with Any working as a stopgap where for the moment no valid type exists, until you finally have a fully typed program. Inserting Anys because you're lazy is like casting things to Object in Java because you're lazy, abusing it like that is just incredibly sloppy and bad code.

If someone is really struggling with this, simply use a linting rule to ban Any in 'mature' code.

Re:Python’s “type hints” are a bit of a disappointment to me by ZephyrBlu on Thursday April 21, 2022 @09:29PM (#31117244)
Like the author said, the weird thing is that it goes both ways. I'm pretty sure if you try to return `any` from a function with an explicit return type in TypeScript you'll get pinged for it.
Re:Python’s “type hints” are a bit of a disappointment to me by derekbaker783 on Thursday April 21, 2022 @09:46PM (#31117339)
Not with any TSC settings I've ever used.
Re:Python’s “type hints” are a bit of a disappointment to me by MBCook on Thursday April 21, 2022 @10:32PM (#31117595)
noImplicitAny is a very common config option that helps catch a ton of bugs. I wouldn’t use TS without it.
Re:Python’s “type hints” are a bit of a disappointment to me by mikewhy on Thursday April 21, 2022 @11:10PM (#31117786)
noImplicitAny is different than what's being discussed (I think). Returning something typed as `any` in a function that has a different return type is totally fine: it's not implicit (you've cast it to any), and it passes (you're saying the type is literally anything after all).

There are lint rules to avoid typing as `any`.

Re:Python’s “type hints” are a bit of a disappointment to me by DangitBobby on Friday April 22, 2022 @02:31AM (#31118861)
There are "no explicit any" linting rules as well, which are a must.
Re:Python’s “type hints” are a bit of a disappointment to me by d0mine on Thursday April 21, 2022 @10:29PM (#31117567)
Don't confuse strong typing and static typing.

Python is strongly typed. It respects types at runtime e.g., `"1"+1` causes TypeError. Python is not statically typed.

For comparison, C is statically typed but it is weakly typed e.g., printf function has no idea about actual types of its arguments (in particular, errors in the format arg may produce non-sensical results silently).

Re:Python’s “type hints” are a bit of a disappointment to me by ledauphin on Thursday April 21, 2022 @05:47PM (#31115586)
This one is self-explanatory.

Can a string be printed? Yes! Then Python is (correctly) allowing typesafe behavior here. Your (incorrect) annotation does not in any way contradict the typesafe-ness of printing a string (or an int). They're both equally printable.

This is just a very, very bad example, plain and simple. It 'looks' bad to a superficial reading, but in practice it demonstrates only what is already known about static typing in Python - it is enforced (or not) separately from the interpreter.

Re:Python’s “type hints” are a bit of a disappointment to me by civilized on Thursday April 21, 2022 @05:51PM (#31115633)
It seems that your point boils down to "Python enforces types at runtime if you define 'enforce types at runtime' not to include 'enforcing that data you declare to be of a certain type actually is of that type'".

Am I the only one baffled by this way of thinking?

Re:Python’s “type hints” are a bit of a disappointment to me by ledauphin on Thursday April 21, 2022 @06:02PM (#31115724)
Python does and always has enforced types at runtime. This is called duck-typing. If you're not familiar with the concept of strong+dynamic, it is easy to see how this could be confusing. This may help. https://stackoverflow.com/questions/2351190/static-dynamic-v...

Static type annotations by definition are not enforced at runtime. This has been true of every language that has ever used static typing, including C, C++, Java, Rust, Typescript... you name it, statically typed languages only enforce their static typing at compile/analysis time.

Yes, it is possible to annotate types in Python incorrectly. It's possible to do this in all other languages that allow type-unsafe behavior (whether natively or via reflection, etc). This may be less common in some languages than in others, but it is fundamentally possible in the vast majority of languages that perform static typing, because those types are fundamentally enforced at analysis time, not runtime.

The author of the article seems to be unfamiliar with these distinctions, and maybe you are too. It's fair to complain that these distinctions are confusing and make things harder for programmers. Nevertheless, very few languages have ever been designed with a type system that acts exactly the same at analysis time and runtime. It's a very difficult problem, partly because these are all abstractions from the perspective of the underlying computer, which only understands boolean logic and integer/floating point math, and has virtually no other notion of types in any formal sense.

Type systems are a complex topic, and as someone who has been paying attention to them for a while, it's frustrating to see uninformed discussion of them show up on Hacker News. That said, it's perfectly understandable that people are confused by these distinctions, and rest assured there's a lot of effort going into developing better languages that suffer _less_ from these issues.

For the everyday working programmer, however, there are currently lots of tradeoffs to be made, and Python's approach to static+dynamic typing is actually pretty usable, all things considered, which is why the community overall has embraced the new static typing despite its blemishes.

Re:Python’s “type hints” are a bit of a disappointment to me by icedchai on Thursday April 21, 2022 @07:18PM (#31116371)
Duck typing is really enforcement of interfaces, not types themselves. Everyone who has passed a string into a Python method expecting a list of strings has run into this. It works, but did you really want to process a character at a time?

Also, some languages like Java do actually enforce types at runtime. If you've ever called a Java method dynamically with the wrong types, you'd know. You can run into ClassCastException, or worse (ClassNotFoundException if you have the wrong classloader.) This is a runtime error.

Re:Python’s “type hints” are a bit of a disappointment to me by kadoban on Thursday April 21, 2022 @10:27PM (#31117559)
> Duck typing is really enforcement of interfaces, not types themselves

A type system is just a set of rules that must be followed. The rules can be as weird or as simple as you want. Just because <int> + <string> is allowed doesn't mean it's not enforcing types.

Not really a criticism, just trying to push to expand your idea of what a type system is.

Re:Python’s “type hints” are a bit of a disappointment to me by the_af on Thursday April 21, 2022 @06:22PM (#31115908)
> Yes, it is possible to annotate types in Python incorrectly. It's possible to do this in all other languages that allow type-unsafe behavior (whether natively or via reflection, etc). This may be less common in some languages than in others, but it is fundamentally possible in the vast majority of languages that perform static typing, because those types are fundamentally enforced at analysis time, not runtime.

It is pretty damn hard to make this mistake in languages that enforce static typing. I mean, you would have to go out of your way to do so. In Python, however, it is trivial to write the wrong type signature or to modify the body of a function so that it no longer matches the signature.

"Fundamentally possible but terribly unlikely, as opposed to the Python way of doing it" would be a more accurate description.

Re:Python’s “type hints” are a bit of a disappointment to me by ledauphin on Thursday April 21, 2022 @06:44PM (#31116101)
I think your (and the article's) argument could be summarized as "static type annotations without automated enforcement by actually running a type checker considered harmful".

Since this argument is not meaningfully different from "comments that are lies considered harmful", it seems fair to expect reasonable people to dismiss it as uninteresting.

Re:Python’s “type hints” are a bit of a disappointment to me by comex on Thursday April 21, 2022 @08:55PM (#31117012)
It is meaningfully different because, as the article says, comments are known to not be machine verified and thus to potentially be wrong. With type hints, they’re usually verified, but not always, so you’re more likely to trust them and get a nasty surprise when they turn out to be wrong.

However, there are other aspects to the article’s argument, such as the fact that it’s easy to end up with type annotations going silently unenforced even if you do run the checker.

Re:Python’s “type hints” are a bit of a disappointment to me by the_af on Thursday April 21, 2022 @06:52PM (#31116167)
> it seems fair to expect reasonable people to dismiss it as uninteresting.

Do you think that's a fair treatment of someone who disagrees with you but has been respectful of your opinion so far?

I realize proglang debates are flamewar territory. But I have been very careful to state I find Python type hints puzzling and less useful than they should be. It is obviously an interesting opinion shared by many others, not just me or the article's author.

As a fan of statically typed languages, I do indeed find some Python programmers engage in a form of Stockholm's syndrome. It usually takes the form of the assertion "I never found a bug that was a type error". Have you or anyone you know ever said this?

In my mind, the opposite statement of the one you attribute to me would be "wishful thinking and a positive attitude is enough to catch mistakes, it's not necessary to have help from automated tooling". In this day and age, I cannot disagree more.

Re:Python’s “type hints” are a bit of a disappointment to me by ledauphin on Thursday April 21, 2022 @07:08PM (#31116287)
I'm very confused by your response. Where did I suggest that I don't like type systems or think automated tooling is not useful?

It's precisely because I do like and use them that I find Python's static type hints to be extremely useful for large Python codebases.

What I hear you saying is "Python does not offer these things". What I hope you hear me saying is "Python does, in fact, offer these things".

Re:Python’s “type hints” are a bit of a disappointment to me by the_af on Thursday April 21, 2022 @07:24PM (#31116413)
Well, you did frame the discussion as:

> I think your (and the article's) argument could be summarized as "static type annotations without automated enforcement by actually running a type checker considered harmful"

It illustrates your mindset, in my opinion. I think type annotations without checking them (or without a satisfying implementation of said type checking) are mostly pointless.

You also left out the part where I remarked on the dismissive tone of your reply.

Re:Python’s “type hints” are a bit of a disappointment to me by reikonomusha on Thursday April 21, 2022 @09:27PM (#31117232)
No, you're not the only one. All of these gymnastics to justify the behavior of the string-declared-int baffle me.

Is it "legally" baffling, i.e., do I understand why this behaves in the way it does purely mechanically, and do I understand the arguments the gymnasts are making? Yes.

But from an idiomatic, colloquial, linguistic, syntactic, programming-historical, or programming-cultural standpoint? Baffled.

Even calling it gradual typing is baffling. It's just adding metadata-that-doesn't-look-like-metadata that sophisticated programs that aren't part of the Python bytecode compiler can use. The definition I know is the same one from Wikipedia:

> Gradual typing is a type system in which some variables and expressions may be given types and the correctness of the typing is checked at compile time [...]

This is definitely not what Python is doing.

Re:Python’s “type hints” are a bit of a disappointment to me by throwaway287391 on Thursday April 21, 2022 @10:16PM (#31117498)
> This is definitely not what Python is doing.

Maybe not by default but there is tooling that makes it work that way — that’s exactly how the python build system works at my company. I don’t know the details but when I “build” python code (creating bytecode pyc files) it produces compiler errors and fails to build if I e.g. try to pass an int to an f(x: str). This has usefully caught bugs before I ran some expensive/time-consuming scripts.

I guess you could say this doesn’t count as it’s a separate program doing the type checking but IMO there’s not a clear distinction between that and e.g. having a first type checking pass in the “same” compiler program.

Re:Python’s “type hints” are a bit of a disappointment to me by reikonomusha on Thursday April 21, 2022 @10:23PM (#31117537)
I don't know the latest but PEP 484 writes:

> While these [type] annotations are available at runtime through the usual __annotations__ attribute, no type checking happens at runtime. Instead, the proposal assumes the existence of a separate off-line type checker which users can run over their source code voluntarily.

Re:Python’s “type hints” are a bit of a disappointment to me by throwaway287391 on Thursday April 21, 2022 @10:30PM (#31117577)
Right, it’s definitely still a voluntary feature. What I meant is if you opt in and make it part of your project’s standard build procedure for python code, you effectively get the behavior and benefits of “compile-time type checking” (to the extent that you actually use type annotations in your code).
Re:Python’s “type hints” are a bit of a disappointment to me by plonk on Friday April 22, 2022 @03:28PM (#31126866)
>> Gradual typing is a type system in which some variables and expressions may be given types and the correctness of the typing is checked at compile time [...] > This is definitely not what Python is doing.

When I read "compile time", I mentally replace it with "before runtime". There isn't a real compilation step in Python, so it's reasonable to accept static code analysis as part of "compile time" in that definition.

The point is that type annotations will let a static analysis tool make sure that the program is correctly typed. This can be enabled easily in most of the modern IDEs and editors. You could just decide to block the code's deployment in CI/CD if the type checker raises issues, for example.

Re:Python’s “type hints” are a bit of a disappointment to me by umanwizard on Thursday April 21, 2022 @09:39PM (#31117302)
This is how all or most statically-typed languages work, too. The typing is purely a compile-time construct.

E.g. this kind of code: https://play.rust-lang.org/?version=stable&mode=debug&editio...

typically works fine (or worse, works fine in debug mode and causes unpredictable UB-related bugs in release mode).

std::mem::transmute here is a no-op at runtime; all it does is subvert the static type checker.

The difference between statically-typed languages and things like Python with type hints is _not_ that the former has any runtime type checking; it’s that subverting the (static!) type system is more difficult and requires more obvious ceremony, and that static type checking is required whereas with Python it’s optional.

If you hacked rustc to disable static typechecking you would get a similar experience to Python with type hints but without any mypy-like tools. (Of course, this would be hard to do in practice, because Rust, like most statically-typed languages, uses types not just for typechecking but also for code generation and method dispatch.)

Re:Python’s “type hints” are a bit of a disappointment to me by the_af on Thursday April 21, 2022 @05:50PM (#31115615)
Agreed. Static typing is usually enforced during the compile stage in other languages. It's specifically meant to catch problems before running the code (hence the "static" in its name).

This said, I find the linting/type-hint-checking stage of Python cumbersome and confusing :(

Re:Python’s “type hints” are a bit of a disappointment to me by the_af on Thursday April 21, 2022 @05:48PM (#31115602)
I agree with most of your comment, but I think there's a miscommunication problem here.

When Python "enforces types at runtime" this is the actual types, not type hints. Type hints are not a runtime artifact. The "actual type" here is "string", and enforcing it means Python would not allow invalid operations on it without error'ing. A programming language that will let you do basically anything to any value because it doesn't enforce type safety is of course C. Python is safer than C.

Re:Python’s “type hints” are a bit of a disappointment to me by AnimalMuppet on Thursday April 21, 2022 @06:52PM (#31116163)
Yes and no. C will let you do things like:

  *(unsigned long*)0xFFFFFF14 = 0x749235f8;
It will not let you do things like:

  *0xFFFFFF14 = 0x749235f8;
or

   char* s = "abc";
   *(unsigned long*)0xFFFFFF14 = s;
It also won't let you call thing.method without thing.method definitely existing.

Best of all, all of these are enforced at compile time.

Now, C absolutely will let you convert an int to a pointer, or a char to an int, or an array to a pointer, or... well, it will let you convert lots of things to lots of things. Some of those things are unsafe unless you are quite sure what you're doing.

> A programming language that will let you do basically anything to any value because it doesn't enforce type safety is of course C.

False on several grounds. You can't call something that isn't a function. You can't call a function on something that doesn't have it. You can't use an int as a pointer or an array without casting it. You can't use an int as a dict (not that C has any built-in idea of what a dict is...)

> Python is safer than C.

Depends on what you're doing, and what kinds of errors you are more prone to.

Re:Python’s “type hints” are a bit of a disappointment to me by woodruffw on Thursday April 21, 2022 @10:11PM (#31117465)
> You can't call something that isn't a function.

Sure you can. That's exactly what you do when you call a function returned by `dlopen` -- it might be a function, but it might also just be garbage. It might even be garbage that's coincidentally marked as executable and does some stuff before eventually crashing!

> You can't call a function on something that doesn't have it.

This is also very easy to do -- you can default-initialize a structure containing function pointers, and then call one of them. You'll be calling some random crap on the stack, which might or might not do anything of interest. But either way, C will happily let you do it.

C isn't completely untyped (there are, as you've observed, things it will forbid at compile time), but it's about as weak as a static typing system can be.

Re:Python’s “type hints” are a bit of a disappointment to me by moefh on Thursday April 21, 2022 @11:45PM (#31117959)
I think you're talking past the comment you're replying to.

In both examples, you have tell the compiler exactly what the types are. In the first one you have to cast the return value of `dlsym` to a function pointer, and at that point, as far as the compiler is concerned, it's a function. In the second example you have to explicitly declare the struct containing function pointers, so of course you can call its members.

So it's strange to suggest that C is somewhat untyped; it's not untyped at all. C just makes it very easy to force the compiler to assume different type for a value (that's necessary for low-level code like the examples in the comment you replied to; and of course that makes it very easy to do unsafe things, I don't think anyone disputes that).

And about the "weak typing system" comment, note that nobody agrees on what exactly that means. Just as an example, see how this page[1] defines "weaker" vs "stronger" types, and how it's completely different from the way you're using here. In general, I find people call type systems that don't do what they expect or want "weak", but that's not an useful discriminator: at that point you might just call it "bad typing" to dispel any illusion that it has any objective meaning.

[1] http://book.realworldhaskell.org/read/types-and-functions.ht...

Re:Python’s “type hints” are a bit of a disappointment to me by woodruffw on Friday April 22, 2022 @12:18PM (#31123622)
I didn't say it was "somewhat untyped." I said it has a very weak static typing system.

Just because "weak typing" means something different in Haskell doesn't mean it can't be used meaningfully (and beyond "bad") in the context of C. C's typing is historically referred to as "weak" because C's notion of casting doesn't distinguish between type and value transmutation: pointer-to-pointer casts don't convert the referent (because they can't), which in turn gives the C compiler very little leeway in proving that the program's types as declared have any particular meaning at runtime.

Compare this to Python, which is "strongly" typed in the same sense: doing `y = str(x)` on `x` means that `type(y)` actually is `str`, and not merely a promise to the runtime. It's enforced, which makes it strong.

Re:Python’s “type hints” are a bit of a disappointment to me by moefh on Friday April 22, 2022 @03:27PM (#31126858)
My point about "weak typing" is this: if someone says "hey, I'm designing a new language, its type system is going to be weak", they gave you no information whatsoever about the type system other than maybe "some people on the Internet will probably complain about it".

That's why it's useless to say things like "it's about as weak as a static typing system can be" and "has been historically considered weak".

If you had explained your objection to C's type system (like you have now), I wouldn't have said anything. And I've been on the Internet long enough to have seen C's typing is historically referred to as "weak" because X for *many* values of X, so I disagree that that's the only or even main objection.

Re:Python’s “type hints” are a bit of a disappointment to me by the_af on Thursday April 21, 2022 @07:00PM (#31116223)
I find this level of nitpicking puzzling.

You know what I meant and what error I was clarifying for the comment I was replying to. Yet you went out of your way to point all sorts of irrelevant mistakes in my post, when the gist of it was right.

Do you feel it was more important to correct me on these trivial things, or was my reply more or less correct when fixing the conceptual error in this sentence?:

> "In what reasonable sense can Python be said to "enforce types at runtime" here?"

Here's a nitpick of my own to your post:

> "Some of those things are unsafe unless you are quite sure what you're doing."

Wrong. Type (un)safety does not depend on you "being quite sure of what you're doing".

PS: assume I know C and that I've programmed complex things using it. No need to catch yourself with statements like "not that C has any idea of what a dict is". Assume we are both C programmers. It will make you sound more polite.

Re:Python’s “type hints” are a bit of a disappointment to me by AnimalMuppet on Thursday April 21, 2022 @07:15PM (#31116353)
1. You might need to re-read the site guidelines about assuming good faith.

2. Your last two sentences were, I think, not needed for the point you were making. They were also somewhere between gross generalizations and flat-out wrong, and felt like a gratuitous slam on a language that wasn't even the topic. I thought that deserved a response, even if it wasn't your main point.

3. I may assume that you know C. I don't assume that everyone reading this exchange knows C.

Re:Python’s “type hints” are a bit of a disappointment to me by the_af on Thursday April 21, 2022 @07:39PM (#31116530)
> "They were also somewhere between gross generalizations"

Pointless correction. Gross generalizations are not wrong in this particular situation, which is not meant to teach anyone C.

Let me try again.

Do you think that your correction was more important than helping the poster of this comment correct their conceptual mistake?

> "In what reasonable sense can Python be said to "enforce types at runtime" here?"

Re:Python’s “type hints” are a bit of a disappointment to me by woojoo666 on Thursday April 21, 2022 @08:58PM (#31117043)
All true but all rather irrelevant to their point
Re:Python’s “type hints” are a bit of a disappointment to me by linsomniac on Thursday April 21, 2022 @08:09PM (#31116721)
Python can't be said to enforce types at runtime. AFAIK, it doesn't say that. In fact, the original article starts off with "I'm expecting to be able to run a static analyzer", then it goes on to say "This isn't caught at runtime".

Yes, but is it caught at static analyzer run? Yes, I typed it into vi and ran mypy on it and it said "error: Incompatible types in assignment (expression has type "str", variable has type "int")."

But I didn't have to run mypy on it, my editor told me: "Expression of type "Literal['hello']" cannot be assigned to declared type "int"".

Maybe the word "educated" wasn't the right choice in this case, can you offer a better one? I honestly couldn't make it through the entire article, despite several attempts. It is full of inconsistencies and what seem like wilful misunderstandings to justify the authors conclusion.

Particularly as the author starts off saying "run a static analysis tool" as if knowing that's how you go about it, and then saying "it doesn't catch it at runtime".

Re:Python’s “type hints” are a bit of a disappointment to me by fer on Friday April 22, 2022 @02:25AM (#31118832)
> Python can't be said to enforce types at runtime.

It does: in form of TypeError exceptions. It doesn't enforce type hints, sure, but that's why they're called hints and not declarations.

Re:Python’s “type hints” are a bit of a disappointment to me by woodruffw on Thursday April 21, 2022 @07:39PM (#31116527)
> In what reasonable sense can Python be said to "enforce types at runtime" here?

Python is both strongly typed and protocol-typed. In the case of `print(...)`, the protocol is that the received parameter responds to `str(...)` (i.e., has `__str__`).

(What Python isn't is statically typed. But "strong" and "static" are completely different typing dimensions.)

Re:Python’s “type hints” are a bit of a disappointment to me by reikonomusha on Thursday April 21, 2022 @09:31PM (#31117253)
I think the person you're responding to means to ask

> In what reasonable sense can Python be said to "enforce statically declared types at runtime" here?

which is certainly an extremely pertinent question for a language that allows declaring supposedly static qualities of a program.

Re:Python’s “type hints” are a bit of a disappointment to me by woodruffw on Thursday April 21, 2022 @10:01PM (#31117413)
> In what reasonable sense can Python be said to "enforce statically declared types at runtime" here?

It can't, because Python doesn't have statically declared types. It's a strong, dynamically typed language.

Python also doesn't allow you to declare static types. It allows you to declare type hints, which are a sort of adjacent and potentially unrealistic (or just plain incorrect) typechecking proof that doesn't correspond at all to runtime behavior.

Re:Python’s “type hints” are a bit of a disappointment to me by reikonomusha on Thursday April 21, 2022 @10:04PM (#31117429)
If your statement were absolutely true then a product like mypy would neither exist nor provide value.

Python has the capacity to have some form of gradual typing built-in at the language level, but it does not, which has led to the enormous confusion we have before us. The SBCL implementation of Common Lisp is a great example of a strong, dynamically typed language that is able to, when possible, detect errors statically at compile time. That tech is 30 years old now.

There is no reason why the Python bytecode compiler could not statically or dynamically detect this programmer error at either compile-time or run-time present in this statement:

    x: int = "x"
Compare to Common Lisp, well known for being dynamically typed (so dynamic that it's possible to change the class of an object at run-time!):

    (declaim (type integer x))
    (defvar x "oops")
    
    ; ==>
    
    Unhandled SIMPLE-TYPE-ERROR in thread #<SB-THREAD:THREAD "main thread" RUNNING {10005D05B3}>:
      Cannot set SYMBOL-VALUE of X to "oops", not of type INTEGER.
    quitting
     
    compilation unit aborted
       caught 1 fatal ERROR condition
Instead, this job in Python is haphazardly relegated to the program readers and IDE implementers.
Re:Python’s “type hints” are a bit of a disappointment to me by woodruffw on Thursday April 21, 2022 @10:24PM (#31117541)
> If your statement were absolutely true then a product like mypy would neither exist nor provide value.

I don't see why that would be the case. The value of Mypy is that it's essentially a proof language over something that resembles Python[1]: it's a way to do some amount of static typing without having to maintain a separate copy and representation of the program.

And sure: Python could detect inconsistent type hints at runtime. But why would it? It's already strongly typed, and the presence of type hints usually means that someone is already running Mypy or another checker during development. It's not clear that there's a significant advantage to be gained, particularly one that justifies the additional overhead.

[1]: "Resembles Python" because mypy does not actually evaluate any Python. It doesn't know what types your program has at runtime; it only knows type hints and a few small rules (for things like string literals) and trusts the developer to reconcile those rules with Python's runtime behavior.

Re:Python’s “type hints” are a bit of a disappointment to me by reikonomusha on Thursday April 21, 2022 @10:27PM (#31117560)
> Python could detect inconsistent type hints at runtime. But why would it?

Because the programmer explicitly intended a contract which is being violated, which would in turn indicate a bug.

Re:Python’s “type hints” are a bit of a disappointment to me by joshuamorton on Friday April 22, 2022 @05:12PM (#31128207)
Do you think other languages should do the same, because very few do any kind of runtime type checking (e.g. bounds checking) due to the performance penalty. Why focus specifically on python's lack of runtime type checking?

Anecdotally, my code in python is functionally fully typechecked and runtime type checking would solely penalize me by slowing down my code. There's no need for it because the type checker correctly proves the soundness of the program's types, in exactly the same way that Java's or C++'s do.

The point is precisely that you don't need to suffer the costs of runtime type checking if you have a solid typechecker. Lisp's approach here is worse than the one adopted by python/js/ts/go/etc.

Re:Python’s “type hints” are a bit of a disappointment to me by joshuamorton on Friday April 22, 2022 @05:07PM (#31128150)
What other languages do that though? C doesn't, Java doesn't outside of particular cases. If you can trick or mislead the compiler, the runtime often has no type information at all, so you'll get, at best, a ClassCastException, and more often than not get a segfault.
Re:Python’s “type hints” are a bit of a disappointment to me by sevensor on Thursday April 21, 2022 @07:37PM (#31116517)
Python enforces its runtime types at runtime. 'hello' is a perfectly valid argument to print. Python will raise a TypeError if you try to use it to index a list though.
Re:Python’s “type hints” are a bit of a disappointment to me by ungawatkt on Thursday April 21, 2022 @06:03PM (#31115736)
For 1, there's some unfortunate but important semantics. Python does not enforce type _Hints_ at runtime, as Hints are in many ways fancy comments. But as pointed out, python is a strongly typed language (at least on the sliding scale of strong to weak), and types are known at runtime (call `print(type(var))` to see them). And calling `1 + 'a'` will result in a TypeError exception, unlike say JavaScript.

I believe this is the relavent passage in the article the GP is referencing, which is incorrect if you take "runtime" to mean something like when the line is executed: "Python is a dynamically typed language. By definition, you don’t know the real types of variables at runtime."

It _is_ however correct to say you don't know the real type _before_ runtime, at least python does not.

Re:Python’s “type hints” are a bit of a disappointment to me by heavyset_go on Thursday April 21, 2022 @09:01PM (#31117063)
> In what reasonable sense can Python be said to "enforce types at runtime" here?

    In [1]: foo: int = 'hello'

    In [2]: foo + 1

    TypeError
    Input In [2], in <cell line: 1>()
    ----> 1 foo + 1

    TypeError: can only concatenate str (not "int") to str
Re:Python’s “type hints” are a bit of a disappointment to me by bscphil on Thursday April 21, 2022 @09:41PM (#31117310)
Exactly right, great example. And furthermore:

    >>> foo: int = 'hello'
    >>> type(foo)
    <class 'str'>
Python's type annotations are annotations. Types are being enforced at runtime, it's just that some are thinking that annotations have some effect on the interpreter, when they do not.
Re:Python’s “type hints” are a bit of a disappointment to me by reikonomusha on Thursday April 21, 2022 @09:41PM (#31117313)
This is a demonstration that it has not enforced what many would reasonably believe is a static type declaration.

But we have (to quote another commenter) "uneducated" people making a supposedly boneheaded mistake to assume

    x: T = y
is a declaration that x will only contain values of type T, or otherwise something will be detected by cpython as invalid (at either bytecode-compile-time or run-time).

Instead, this line is more properly interpreted as "documentation reasonably believed to represent a type saying something about values that x can be assigned at runtime, that's not in a docstring, that some programs may query."

I can totally forgive anybody who believes the former over the latter, and expresses their belief in a statement like that which you've quoted.

Re:Python’s “type hints” are a bit of a disappointment to me by woodruffw on Thursday April 21, 2022 @10:14PM (#31117487)
Type hints are not static type declarations. Python does not refer to them as such, nor does Mypy or any other typechecker for Python type hints.

Absent of active machine checking (e.g., via mypy), type hints should be considered exactly what you said: another form of documentation, one that might just be wrong.

Re:Python’s “type hints” are a bit of a disappointment to me by reikonomusha on Thursday April 21, 2022 @10:17PM (#31117504)
This is my point precisely: it looks like a static type declaration to anybody who has seen what a static type declaration looks like in any statically or gradually typed program in the last 50 years.

But in Python, it's not (and documented in a PEP as not), and it causes massive confusion.

Re:Python’s “type hints” are a bit of a disappointment to me by heavyset_go on Friday April 22, 2022 @03:52AM (#31119279)
That's the way it works in Ruby with RBS, and it will work the same way in JavaScript should Microsoft's proposal for type hints make it into ECMAScript[1].

[1] https://devblogs.microsoft.com/typescript/a-proposal-for-typ...

Re:Python’s “type hints” are a bit of a disappointment to me by sigzero on Thursday April 21, 2022 @09:43PM (#31117324)
Yeah, you are talking about actual types and not type hints. It gets confusing. Python doesn't enforce type hints at runtime.
Re:Python’s “type hints” are a bit of a disappointment to me by smohare on Friday April 22, 2022 @12:50AM (#31118325)
Come on. It’s uneducated because it’s just a naive hot take. The education in this case comes from experience and basic understanding of the underlying issues. The author does not demonstrate possessing either of these attributes.
Re:Python’s “type hints” are a bit of a disappointment to me by bayesian_horse on Friday April 22, 2022 @12:21AM (#31118184)
The "print" function will take any Python object and call its "__str__" method. Not sure if it will do it to a string, but the outcome would be the same.

As far as I remember, the "__str__" method is present on all objects in some form, so print will happily take any type.