-
Notifications
You must be signed in to change notification settings - Fork 41
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
Incompatible types in assignment when base declaration is in a separate file #264
Comments
Hi, Are you using the mypy plugin? That may be an issue with that and I'm not sure it's worth fixing |
Yes I'm using the mypy plugin The reason I need this pattern is that we have a common base class with some fields defined for every table:
|
Don't know honestly. SQLAlchemy v2 has moved on wrt the plugin, that's now basically deprecated, along with these stubs. If you want to have use types in any meaningful way I would suggest migrating to v2 |
That is definitely the plan, I was just hoping to continue to use the proper types during the migration. Perhaps that won't be possible. |
I am in the kind of same situation. Also to fix the RemoveIn20Warnings, it requires to make changes that break the types from the (old stubs). The idea by using the "not recommended" stubs from this repo would be to:
The codebase can not quickly upgrade to 2.0 so it has to live in this grey area for quite some time as it is big. I also tried to bypass the inferred types by using
|
I guess you could try doing something like this, not sure if it will help: if TYPE_CHECKING:
Base = declarative_base()
else:
from base_file import Base @Morikko not sure, upgrading a 1.4 codebase to v2 should not require too much effort, since all the typing related changes are all optional and can be added later on (or never) |
After a second analysis, I found the root cause. Initially, the code used the declarative way The solution was to use the class way directly, eg the transformation the plugin is doing. (Creating an Explicit Base Non-Dynamically (for use with mypy, similar) class Base(metaclass=DeclarativeMeta):
... However, the mapping was not working because the plugin is very picky on using the exact name while the code was using: class DeclarativeABCMeta(sqlalchemy.orm.decl_api.DeclarativeMeta, abc.ABCMeta):
...
class Base(metaclass=DeclarativeABCMeta):
... In the meanwhile, I managed to fix the issues related to the declarative way usage so the mappings are generated as expected again. So it is a bit different from the original issue where the |
Scenario that works as expected:
test_db.py
:Command:
pipenv run mypy --follow-imports=skip test_db.py
Result:
Everything passes, yay! 🎉
Failure scenario:
test_db.py
:base_file.py
:Command plus output:
Versions.
The text was updated successfully, but these errors were encountered: