Skip to content
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

Inconsistent error when aliasing a type from a module without stubs #2801

Open
frankpf opened this issue Feb 3, 2017 · 3 comments
Open

Inconsistent error when aliasing a type from a module without stubs #2801

frankpf opened this issue Feb 3, 2017 · 3 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@frankpf
Copy link

frankpf commented Feb 3, 2017

test_mypy.py:

import pymongo # this can be any third-party module without stubs

Collection = pymongo.collection.Collection

def func(a: Collection) -> bool:
   return True

Here's the output of mypy test_mypy.py :

test_mypy.py:1: error: No library stub file for module 'pymongo'
test_mypy.py:1: note: (Stub files are from https://github.com/python/typeshed)
test_mypy.py: note: In function "func":
test_mypy.py:5: error: Invalid type "test_mypy.Collection"

However, if I change test_mypy.py to this:

import pymongo

def func(a: pymongo.collection.Collection) -> bool:
  return True

then mypy does not complain about pymongo.collection.Collection being an invalid type. This seems inconsistent/wrong, Collection is not an invalid type, it's just an alias for a type without a stub.

@frankpf
Copy link
Author

frankpf commented Feb 3, 2017

This seems similar to #2075.

@gvanrossum
Copy link
Member

Looks like it doesn't treat Collection = pymongo.collection.Collection as a type alias definition? In the second example, pymongo is an object of type Any and attributes thereof are also treated as Any.

Agreed this should be dealt with better.

@JukkaL JukkaL added bug mypy got something wrong priority-1-normal labels Feb 7, 2017
@JukkaL JukkaL added the false-positive mypy gave an error on correct code label Jan 11, 2019
@JukkaL
Copy link
Collaborator

JukkaL commented Jan 11, 2019

I've hit this issue as well. It's not clear if Collection should be treated as a type alias, since it could also be an ordinary variable. Mypy should perhaps behave as if there was an explicit Any annotation. This doesn't generate an error:

...
Collection: Any = pymongo.collection.Collection  # Note type annotation!

def func(a: Collection) -> bool:  # OK
   return True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

No branches or pull requests

3 participants