Question about a possible improvement to the behaviour of unspecified Generic arguments.
Hello, in debugging some old code of mine i encountered an issue where an attribute i would have expected to have had a type instead was Any, causing type-checking to ignore a possible bug in my code. Consider the following example code: --- from typing import TypeVar, Sized T = TypeVar('T', bound=Sized) class Foo(Generic[T]): def __init__(self, value: T) -> None: self.value: T = value def takes_foo(foo: Foo) -> None: len(list(reversed(foo.value))) --- Testing with mypy and pyright, by default both will type-check, however this code is unsound, foo.value may not be Reversible. This happens because as PEP 484 [1] states: --- Additionally, Any is a valid value for every type variable Consider the following: def count_truthy(elements: List[Any]) -> int: return sum(1 for elem in elements if elem) This is equivalent to omitting the generic notation. --- So the type of foo.value is Any and from that point on, everything type-checks. So I ask this: Is there any reason why type-checkers treating omitted generics as the TypeVar bound, if set, rather than Any could be problematic? Doing so would remove the chance of errors such as these from ocurring in a lot of situations. Interested to hear thoughts on this, thanks. [1] - https://www.python.org/dev/peps/pep-0484/#generics
Pyright offers an option called `reportMissingTypeArgument`. It is disabled by default with "basic" type checking but on by default with "strict" type checking. I recommend enabling this setting to avoid problems like this. This would have highlighted the fact that you had omitted the type argument for `Foo`, which should be `Foo[Sized]` in this case. I don't know if mypy has an equivalent setting. There have been some discussions in the past of supporting "default type argument values" for TypeVars. This would allow you to specify a type other than `Any` that should be used when a type argument is omitted. Eric Traut Contributor to Pyright & Pylance
Mypy supports --disallow-any-generics which complains about missing type
arguments.
Jukka
On Tue, Mar 1, 2022 at 5:10 PM Eric Traut
Pyright offers an option called `reportMissingTypeArgument`. It is disabled by default with "basic" type checking but on by default with "strict" type checking. I recommend enabling this setting to avoid problems like this. This would have highlighted the fact that you had omitted the type argument for `Foo`, which should be `Foo[Sized]` in this case. I don't know if mypy has an equivalent setting.
There have been some discussions in the past of supporting "default type argument values" for TypeVars. This would allow you to specify a type other than `Any` that should be used when a type argument is omitted.
Eric Traut Contributor to Pyright & Pylance _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: jlehtosalo@gmail.com
participants (3)
-
Eric Traut
-
Joshua Butt
-
Jukka Lehtosalo