Skip to content

Omitted Generics Should Be Disallowed #3469

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

Closed
ilinum opened this issue May 27, 2017 · 6 comments
Closed

Omitted Generics Should Be Disallowed #3469

ilinum opened this issue May 27, 2017 · 6 comments

Comments

@ilinum
Copy link
Collaborator

ilinum commented May 27, 2017

Currently, if a generic type parameter is not specified, it is assumed to be Any (List becomes List[Any]).

Forgetting to provide a generic type is an easy mistake to make, which makes mypy less effective at typechecking as the generic type will be inferred as Any. We should make sure e.g. List[Any] is actually what user wants by making the user explicitly state List[Any] as the type.
If the generic parameter is not specified, mypy should output an error.

There’s also the problem of list, set, etc. We should probably forbid these too and give a nice error message saying they should use List. However, this part of the proposal requires more consideration since, for instance, it’s possible that the user does not want to import typing module.

As a migration aid, we can provide a flag to allow implicit any from omitted generics but eventually we should remove it too.

@gvanrossum
Copy link
Member

Well, to make this the default/only way, you would have to propose a change to PEP 484 and get pytype and PyCharm to agree. And there may be a lot of code to update.

@ilevkivskyi
Copy link
Member

I am against making this the default. FWIW I have seen lots of code on GitHub that just uses plain list, dict etc. in annotations. I could agree with emitting a note by default (like note: bare "MyGeneric" is interpreted as "MyGeneric[Any, Any]".) but keeping the exit code 0 in this case, with an option to make this note an error. This would be similar to how many compilers work.

@ilinum
Copy link
Collaborator Author

ilinum commented May 30, 2017

yeah, these are good points.
After some in person discussion with @gvanrossum, @JukkaL, and @ddfisher, we decided will add a flag (perhaps in #3141) to forbid it.
Then, we may or may not move forward with making omitted generics an error by default depending on how well the adoption of the new flag will go.

@ilinum
Copy link
Collaborator Author

ilinum commented May 30, 2017

@ilevkivskyi, I think that may be a good idea but at the same time it may flood the user's console with such errors. Of course, we can do what javac does: emit a line saying that there are unchecked operations and provide a flag for verbose output about these operations.

@ddfisher
Copy link
Collaborator

I think the right place in the tradeoff space may be to allow e.g. list (perhaps with a note on the console) but to disallow List (as you should just write List[Any] instead, because it's clearer and ensures you understand what it means).

@ilevkivskyi
Copy link
Member

I think this is now fixed, since we have --disallow-any=generics

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

No branches or pull requests

4 participants