-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Option to disallow omitting type parameters of generic types #3637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure this is the best error message. Feedback and ideas are appreciated!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about something like: error: Implicit generic Any. Use 'typing.Tuple' and specify generic parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds pretty good! Will add.
test-data/unit/cmdline.test
Outdated
@@ -681,3 +681,191 @@ from unreal import F | |||
|
|||
def f(x: F) -> None: pass | |||
[out] | |||
|
|||
[case testDisallowAnyGenericsTupleNoTypeParams] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking for more test ideas!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a pretty good set to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about something like: error: Implicit generic Any. Use 'typing.Tuple' and specify generic parameters.
test-data/unit/cmdline.test
Outdated
@@ -681,3 +681,191 @@ from unreal import F | |||
|
|||
def f(x: F) -> None: pass | |||
[out] | |||
|
|||
[case testDisallowAnyGenericsTupleNoTypeParams] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added!
# 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() -> List
would produce an error whiledef x() -> List[Any]
is allowed.Note that with the flag enabled builtin generic types such as
list
andset
are also forbidden.