18 ms·
Type annotations are just like documentation though. Just because the annotation says int the function can still return a list.
by tayo42 2y ago
Type annotations are just like documentation though. Just because the annotation says int the function can still return a list.
- maleldil 2y agoAnnotations can and should be checked. If I change a parameter type, other code using the function will now show errors. That won't happen with just documentation.
- tayo42 2y agoIn some cases don't you need to actually execute the code to know what the type actually is. How does the type checker know then?
- maleldil 2y agoIt doesn't. There are cases where the type-checker can't know the type (e.g. json.load has to return Any), but there are tools in the language to reduce how much that happens. If you commit to a fully strictly-typed codebase, it doesn't happen often.
- sevensor 2y agoYou can actually annotate the return type of json.load better than that: JSON = float | bool | int | str | None | list[“JSON”] | dict[str, “JSON”]
- pansa2 2y ago> Annotations can and should be checked Unfortunately Python’s type system is unsound. It’s possible to pass all the checks and yet still have a function annotated `int` that returns a `list`.
- __MatrixMan__ 2y agoDo you mean that you're allowed to only use types where you want to, which means maybe the type checker can't check in cases where you haven't hinted enough, or is there some problem with the type system itself?
- pansa2 2y agoThe type system itself is unsound. For example, this code passes `mypy --strict`, but prints `<class 'list'>` even though `bar` is annotated to return an `int`: i : int | list[int] = 0 def foo() -> None: global i i = [] def bar() -> int: if isinstance(i, int): foo() return i return 0 print(type(bar()))
- _dain_ 2y agoSo don't do this then? The type system does not have to be sound to be useful; Typescript proves this.
- pansa2 2y ago> So don't do this then? Don't do what? - Don't write unsound code? There's no way to know until you run the program and find out your `int` is actually a `list`. - Don't assume type annotations are correct? Then what's the point of all the extra code to appease the type checker if it doesn't provide any guarantees?