3 ms·
Try: def f(i=0) -> None: reveal_type(i) The inferred type is not `float` nor `int`, but `Any`. Mypy will happily let you call `f("some string")`.
by hk__2 1y ago
Try:
def f(i=0) -> None:
reveal_type(i)
The inferred type is not `float` nor `int`, but `Any`. Mypy will happily let you call `f("some string")`.
- veber-alex 1y agoThat's a mypy issue. Pyright correctly deduces the type as int. In any case it's a bad example as function signatures should always be typed.
- jsjohnst 1y agoSo you want strong typing, but then are to lazy to properly type your function definitions?
- toolslive 1y agono need to explicitly write the type if you have type inference: > # fun x -> x + 1;; > - : int -> int = <fun> >
- jsjohnst 1y ago1) the code you wrote isn’t Python. 2) inferring the type is int isn’t guaranteed to be correct in this case
- toolslive 1y agoI was merely giving an example that strong typing has nothing to do with having to write the types. (and, obviously, the inferred type (int -> int) is correct. )
- Izkata 1y agoOnly if reveal_type only accepts an int. Just because the default value of i is 0 doesn't mean anything about what could be passed in.
- toolslive 1y agonot my fault python is broken.
- jsjohnst 1y ago> and, obviously, the inferred type (int -> int) is correct. No it’s not. It’s Optional[int] -> int at minimum. There are other completely valid signatures beyond that too.
- int_19h 1y agoIt's guaranteed to be correct if you use different operators for ints and floats, which is what at least some ML dialects (notably, OCaml) do precisely so that types can be inferred from usage. That's the downside of operator overloading - since it relies on types to resolve, they need to be known and can't be inferred.
- hk__2 1y agoI want a typing system with a good inference that doesn’t require me to type each and every variable, just like in any good statically-typed language like OCaml or Typescript. Strong typing and explicit typing are two very different things.
- mzl 1y agoHow would a typing system know if the right type is `int` or `Optional[int]` or `Union[int, str]` or something else? The only right thing is to type the argument as `Any` in the absence of a type declaration.
- hk__2 1y agoThe typing system should use the most specific valid type, and the code author can broaden it with explicit typing if needed. No good typing system should ever infer a variable as `Any`: "I don’t know the type of this" (`unknown` in TypeScript) is not the same thing as "This function accepts anything". Conflating these things is one of the main reasons why Mypy is so annoying.
- mzl 1y agoA typing system should only infer things that it knows are true, it should never invent restrictions. In a language like Python that is duck-typed, `Any` is the only reasonable choice in the absence of other real constraints like a type-annotation.
- rfoo 1y ago`Any` is the correct call. It could be: def f(i=0) -> None: if i is None: do_something() else: do_something_else() Yeah, I know it's retarded. I don't expect high quality code in a code base missing type annotation like that. Assuming `i` is `int` or `float` just makes incrementally adoption of a type checker harder.
- hk__2 1y agoNo it’s not. The typing system should use the most specific type available, and it’s your responsability to broaden it if needed. That’s how it works in all statically-typed languages.