Skip to content

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

Open

Description

@dgoldstein0

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.

Activity

  1. ilevkivskyi commented on Apr 20, 2017

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

  2. dgoldstein0 commented on Apr 20, 2017

    @dgoldstein0
    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

  3. ilevkivskyi commented on Apr 20, 2017

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

  4. 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[/+] on Apr 20, 2017
  5. added a commit that references this issue on Apr 27, 2017
  6. self-assigned this
    on May 18, 2017
  7. ddfisher commented on May 18, 2017

    @ddfisher
    Collaborator

    (assigning to myself to hold for @ilinum)

  8. ddfisher commented on May 18, 2017

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

  9. added 3 commits that reference this issue on May 22, 2017
    54bb2fa
    704a7b7
    29848f8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions