Skip to content

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

Merged
merged 4 commits into from
Jul 5, 2017

Conversation

ilinum
Copy link
Collaborator

@ilinum ilinum commented Jun 30, 2017

(--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 while
def x() -> List[Any] is allowed.

Note that with the flag enabled builtin generic types such as list and set
are also forbidden.

(--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.
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
Copy link
Collaborator Author

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!

Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

@ilinum ilinum mentioned this pull request Jun 30, 2017
@@ -681,3 +681,191 @@ from unreal import F

def f(x: F) -> None: pass
[out]

[case testDisallowAnyGenericsTupleNoTypeParams]
Copy link
Collaborator Author

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!

Copy link
Collaborator

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!

Copy link
Collaborator

@ddfisher ddfisher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

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
Copy link
Collaborator

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.

@@ -681,3 +681,191 @@ from unreal import F

def f(x: F) -> None: pass
[out]

[case testDisallowAnyGenericsTupleNoTypeParams]
Copy link
Collaborator

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)
Copy link
Member

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!

ilinum added 3 commits July 5, 2017 13:19
# Conflicts:
#	mypy/main.py
#	mypy/messages.py
#	mypy/semanal.py
#	mypy/typeanal.py
#	mypy/types.py
#	test-data/unit/cmdline.test
@ilinum ilinum merged commit 324f68f into python:master Jul 5, 2017
@ilinum ilinum deleted the disallow-any=generics branch July 5, 2017 20:52
@ilinum ilinum mentioned this pull request Jul 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants