-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Bug Report
Sorry in advance, this is going to be an non-ideal bug report. I've spent over an hour now trying to figure out how to create a simpler project that reproduces the issue, but I haven't been able to cause it to happen, so I'm going to have to use my real project as the example.
I recently updated mypy from 0.790 to 0.910, and it started giving a number of [valid-type]
errors related to some of my classes, saying that I couldn't use them as type annotations. Here's one example:
tildes/models/group/group_subscription.py: note: In class "GroupSubscription":
tildes/models/group/group_subscription.py:43: error: Module
"tildes.models.user.User" is not valid as a type [valid-type]
user: User = relationship("User", innerjoin=True, backref="subscri...
^
I had to make some different modifications to how I was importing the classes to get rid of these errors:
- If the class was from inside the same package, it had to be imported like
from .group import Group
. Using the full path likefrom tildes.models.group import Group
would result in an error. - In one specific package, I had to specify the import even more exactly. These ones required me to do
from tildes.models.user.user import User
, where every other "sibling" package can dofrom tildes.models.user import User
without mypy complaining (the class is imported in the__init__.py
to allow that).
All of the changes I needed to make are in this commit if you want to look (there are also some fixes for legitimate typing issues in there too).
The second type of change above is the more interesting one, because I have some modules that are very close to identical, but require different import styles to make mypy happy.
If you compare these two files:
They're almost identical - the first one is a link between User
+Group
, and the other is a link between User
+Topic
. However, mypy needs me to do the from tildes.models.user.user import User
import in group_subscription.py
, but is fine with from tildes.models.user import User
in topic_vote.py
.
To Reproduce
(Sorry again for having to use my actual project here)
- Clone https://gitlab.com/tildes/tildes
- Open terminal inside the repo directory
git checkout 9720040cb9a7c75fd590322479e821a0e8f13497 -- tildes/tildes/models/group/group_subscription.py
(reverts the fix I made to that single file)- Change into the
tildes/
subdirectory, and runmypy .
- Get 4 errors, two of which are from files other than the one that was reverted:
tildes/models/user/user_permissions.py: note: In class "UserPermissions":
tildes/models/user/user_permissions.py:31: error: Module "tildes.models.group.Group" is not valid as a
type [valid-type]
group: Group = relationship("Group", innerjoin=True)
^
tildes/models/user/user_group_settings.py: note: In class "UserGroupSettings":
tildes/models/user/user_group_settings.py:34: error: Module "tildes.models.group.Group" is not valid as
a type [valid-type]
group: Group = relationship("Group", innerjoin=True)
^
tildes/models/group/group_subscription.py: note: In class "GroupSubscription":
tildes/models/group/group_subscription.py:43: error: Module "tildes.models.user.User" is not valid as a
type [valid-type]
user: User = relationship("User", innerjoin=True, backref="subscriptions")
^
tildes/models/group/group_subscription.py: note: In member "__init__" of class "GroupSubscription":
tildes/models/group/group_subscription.py:46: error: Module "tildes.models.user.User" is not valid as a
type [valid-type]
def __init__(self, user: User, group: Group):
^
Found 4 errors in 3 files (checked 145 source files)
Expected Behavior
mypy should support classes imported in any of the ways Python supports.
Actual Behavior
mypy seems to require specific import methods in certain cases.
Your Environment
- Mypy version used: 0.910
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini
(and other config files): https://gitlab.com/tildes/tildes/-/blob/31afe0a8ba574988be4e879c9ab33792ceec9533/tildes/mypy.ini - Python version used: 3.9.5
- Operating system and version: Debian 10