Skip to content

Option to disallow omitting type parameters of generic types#3637

Merged
ilinum merged 4 commits intopython:masterfrom
ilinum:disallow-any=generics
Jul 5, 2017
Merged

Option to disallow omitting type parameters of generic types#3637
ilinum merged 4 commits intopython:masterfrom
ilinum:disallow-any=generics

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
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.

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