Skip to content

Support collection_class in relationship() #107

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
NargiT opened this issue Sep 13, 2019 · 7 comments
Open

Support collection_class in relationship() #107

NargiT opened this issue Sep 13, 2019 · 7 comments

Comments

@NargiT
Copy link

NargiT commented Sep 13, 2019

I do not know if #82 will fix the issue but I have the following problem with mypy. I use typing in order to simplify coding.

repositories: Set['Repository'] = relationship('Repository', back_populates="installation", uselist=True, collection_class=set)

This is the error I get when running mypy with sqlalchemy-stubs

error: Incompatible types in assignment (expression has type "RelationshipProperty[Iterable[Repository]]", variable has type "Set['Repository']")

I also tried with InstrumentedSet which is the instance returned by relationship in my case. but no success. The code works btw, so it's really mypy that is not able to understand.

Do you think it's the scope of sqlalchemy-stubs ?

Information

python 3.6
sqlalchemy 1.3.8 Database Abstraction Library
sqlalchemy-stubs 0.2 SQLAlchemy stubs and mypy plugin
├── mypy >=0.660
│   ├── mypy-extensions >=0.4.0,<0.5.0 
│   ├── typed-ast >=1.4.0,<1.5.0 
│   └── typing-extensions >=3.7.4 
│       └── typing >=3.7.4 
└── typing-extensions >=3.6.5
    └── typing >=3.7.4
@NargiT NargiT changed the title Will it solve my issue ? relationship is to magical Sep 13, 2019
@ilevkivskyi
Copy link
Contributor

The code works btw, so it's really mypy that is not able to understand.

x: int = 'no way' also works at runtime so not really an explanation. Have tried adding a precise annotation to that line? For example:

repositories: RelationshipProperty[Set['Repository']] = relationship('Repository', back_populates="installation", uselist=True, collection_class=set)

That one should work I hope, otherwise it is a bug in sqlalchemy-stubs.

@NargiT
Copy link
Author

NargiT commented Sep 17, 2019

I will try tomorrow with your suggestion. but I am afraid that it will not behave correctly for intellisense

@NargiT
Copy link
Author

NargiT commented Sep 20, 2019

The code

repositories: RelationshipProperty[Set['Repository']] = relationship('Repository', back_populates="installation", uselist=True, collection_class=set)

The error

error: Incompatible types in assignment (expression has type "RelationshipProperty[Iterable[Repository]]", variable has type "RelationshipProperty[Set[Repository]]")

@ilevkivskyi
Copy link
Contributor

OK, the latter error can be fixed by adding support for collection_class. On one hand this doesn't look like high priority (there is a simple workaround like adding correct annotation plus a # type: ignore), but on other hand this should not be hard to fix.

@ilevkivskyi ilevkivskyi changed the title relationship is to magical Support collection_class in relationship() Sep 24, 2019
@NargiT
Copy link
Author

NargiT commented Sep 26, 2019

may you give more information about the "correct annotation" ?

@ilevkivskyi
Copy link
Contributor

repositories: RelationshipProperty[Set['Repository']] = relationship('Repository', back_populates="installation", uselist=True, collection_class=set)  # type: ignore

@NargiT
Copy link
Author

NargiT commented Oct 7, 2019

thx I finally had the time to try this, and I have a funny case again.

the annotation # type: ignore is line sensitive

this does not work

repositories: Set['Repository'] = relationship('Repository', back_populates="installation", uselist=True, collection_class=set,
                                                   cascade="all, delete-orphan") # type: ignore

error

src/models.py:30: error: Incompatible types in assignment (expression has type "RelationshipProperty[Iterable[Repository]]", variable has type "Set[Repository]")

this works

repositories: Set['Repository'] = relationship('Repository', back_populates="installation", uselist=True, collection_class=set,  # type: ignore
                                                   cascade="all, delete-orphan")

looks like the annotation has to be on the same line instead of the instruction. Is this wanted behaviour ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants