Skip to content

Add flag prohibiting annotation by Any resulting from an unanalyzed module #3205

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

Open
dgoldstein0 opened this issue Apr 20, 2017 · 5 comments
Assignees
Labels

Comments

@dgoldstein0
Copy link

example code, in py2.7

import MyType from unanalyzed

def foo(bar):
  # type: (MyType) -> None
  ...

I would expect this to be an error: if mypy doesn't know what MyType is, it shouldn't be a valid type. Instead, it assumes it's Any which is way more permissive behavior than I expect.

@ilevkivskyi
Copy link
Member

This is intentional. Everything non-annotated/unknown should have type Any by default. This is one of the key ideas of gradual typing, otherwise annotating a large codebase would be a non-starter.

For more control on mypy behaviour, you could use flags, for example:

--disallow-untyped-calls
--disallow-untyped-defs
--check-untyped-defs
--disallow-subclassing-any
--warn-return-any

I am closing this, since this is unlikely to change. Feel free to re-open if you have additional concerns.

@dgoldstein0
Copy link
Author

so I understand that it is expected for unchecked imports to be Any - but what I did not expect is that we could use one of them as a type without any warning. It doesn't sound like any of those flags you are suggesting will fix this? Can we please reopen? (It doesn't look like I have the ability to do this myself)

anyhow I filed this task at the suggestion of @ddfisher, who thought a flag for a warning about this could be valuable

@ilevkivskyi
Copy link
Member

but what I did not expect is that we could use one of them as a type without any warning. It doesn't sound like any of those flags you are suggesting will fix this?

Yes, this case is not covered by current flags. And it looks like such flag is not too difficult to implement.

@ilevkivskyi ilevkivskyi reopened this Apr 20, 2017
@ilevkivskyi ilevkivskyi changed the title using a type from an unanalyzed file to be a mypy error Add flag prohibiting annotation by Any resulting from an unanalyzed module Apr 20, 2017
pkch added a commit to pkch/mypy that referenced this issue Apr 27, 2017
ilevkivskyi pushed a commit that referenced this issue Apr 27, 2017
* Revert PR #3205 and #3235

* Stop redirecting to _importlib_modulespec
@ddfisher ddfisher self-assigned this May 18, 2017
@ddfisher
Copy link
Collaborator

(assigning to myself to hold for @ilinum)

@ddfisher
Copy link
Collaborator

As some additional background, this happens when the --follow-imports=skip and --ignore-missing-imports flags are set.

To implement this, I'd start by adding an additional member to the AnyType class which tracks whether or not it's from a silent import. I'd then grep for ignore_missing_imports and follow_imports to find where ignored module imports are turned into Anys, and set the member I'd added to AnyType there.
Next, I'd add the new --disallow-implicit-any-types flag. It needs to be added to process_options in main.py and to Options in options.py. It should be a per-module option.
For the warning itself, I'd add checks to visit_func_def, visit_overloaded_func_def and visit_class_def in checker.py.
Finally, I'd add several tests for this functionality to the end of check-flags.test.

ilinum added a commit to ilinum/mypy that referenced this issue May 22, 2017
These types can appear from an unanalyzed module.
If mypy encounters a type annotation that uses such a type,
it will report an error.
Fixes python#3205
ilinum added a commit to ilinum/mypy that referenced this issue May 22, 2017
These types can appear from an unanalyzed module.
If mypy encounters a type annotation that uses such a type,
it will report an error.
Fixes python#3205
ilinum added a commit to ilinum/mypy that referenced this issue May 22, 2017
These types can appear from an unanalyzed module.
If mypy encounters a type annotation that uses such a type,
it will report an error.
Fixes python#3205
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants