Option to disallow omitting type parameters of generic types#3637
Option to disallow omitting type parameters of generic types#3637ilinum merged 4 commits intopython:masterfrom
Conversation
(--disallow-any=generics) This code is based on python#3141 by pkch. This option disallows implicit Anys from omitted type parameters to generic types. For instance, `def x() -> List` would produce an error while `def x() -> List[Any]` is allowed. Note that with the flag enabled builtin generic types such as `list` and `set` are also forbidden.
test-data/unit/cmdline.test
Outdated
| def i(s: set) -> None: pass | ||
| def j(s: frozenset) -> None: pass | ||
| [out] | ||
| m.py:3: error: Builtin generic types are disallowed. Use 'typing.Tuple' instead |
There was a problem hiding this comment.
I am not sure this is the best error message. Feedback and ideas are appreciated!
There was a problem hiding this comment.
What about something like: error: Implicit generic Any. Use 'typing.Tuple' and specify generic parameters.
There was a problem hiding this comment.
That sounds pretty good! Will add.
| def f(x: F) -> None: pass | ||
| [out] | ||
|
|
||
| [case testDisallowAnyGenericsTupleNoTypeParams] |
There was a problem hiding this comment.
Looking for more test ideas!
There was a problem hiding this comment.
This looks like a pretty good set to me!
test-data/unit/cmdline.test
Outdated
| def i(s: set) -> None: pass | ||
| def j(s: frozenset) -> None: pass | ||
| [out] | ||
| m.py:3: error: Builtin generic types are disallowed. Use 'typing.Tuple' instead |
There was a problem hiding this comment.
What about something like: error: Implicit generic Any. Use 'typing.Tuple' and specify generic parameters.
| def f(x: F) -> None: pass | ||
| [out] | ||
|
|
||
| [case testDisallowAnyGenericsTupleNoTypeParams] |
There was a problem hiding this comment.
This looks like a pretty good set to me!
mypy/typeanal.py
Outdated
| node = self.lookup_fqn_func(fully_qualified_name) | ||
| assert isinstance(node.node, TypeInfo) | ||
| return Instance(node.node, args or []) | ||
| return Instance(node.node, args or [], line=line, column=column) |
There was a problem hiding this comment.
Maybe while you at it you can also fix type arguments here too?
args or [AnyType()] * len(node.node.defn.type_vars)So that we will never create instances with wrong number of type arguments (like with tuple above on line 180)
This could lead to nasty crashes like #3117
# Conflicts: # mypy/main.py # mypy/messages.py # mypy/semanal.py # mypy/typeanal.py # mypy/types.py # test-data/unit/cmdline.test
(--disallow-any=generics)
This code is based on #3141 by pkch.
This option disallows implicit Anys from omitted type parameters to generic types.
For instance,
def x() -> Listwould produce an error whiledef x() -> List[Any]is allowed.Note that with the flag enabled builtin generic types such as
listandsetare also forbidden.