Skip to content

Type relationship as list #154

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

Merged
merged 3 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sqlmypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,10 @@ class User(Base):
# Something complex, stay silent for now.
new_arg = AnyType(TypeOfAny.special_form)

# We figured out, the model type. Now check if we need to wrap it in Iterable
# We figured out, the model type. Now check if we need to wrap it in List
if uselist_arg:
if parse_bool(uselist_arg):
new_arg = ctx.api.named_generic_type('typing.Iterable', [new_arg])
new_arg = ctx.api.named_generic_type('builtins.list', [new_arg])
else:
if has_annotation:
# If there is an annotation we use it as a source of truth.
Expand Down
12 changes: 5 additions & 7 deletions test/test-data/sqlalchemy-plugin-features.test
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ Base = declarative_base()
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship, RelationshipProperty
from typing import Iterable

Base = declarative_base()

Expand All @@ -144,7 +143,7 @@ class User(Base):

user = User()
reveal_type(user.first_other) # N: Revealed type is 'main.Other*'
reveal_type(user.second_other) # N: Revealed type is 'typing.Iterable*[main.Other]'
reveal_type(user.second_other) # N: Revealed type is 'builtins.list*[main.Other]'

class Other(Base):
__tablename__ = 'other'
Expand All @@ -155,7 +154,6 @@ class Other(Base):
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship, RelationshipProperty
from typing import Iterable

Base = declarative_base()

Expand All @@ -169,7 +167,7 @@ class User(Base):

user = User()
reveal_type(user.first_other) # N: Revealed type is 'main.Other*'
reveal_type(user.second_other) # N: Revealed type is 'typing.Iterable*[main.Other]'
reveal_type(user.second_other) # N: Revealed type is 'builtins.list*[main.Other]'

class Other(Base):
__tablename__ = 'other'
Expand All @@ -180,17 +178,17 @@ class Other(Base):
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship, RelationshipProperty
from typing import Iterable
from typing import List

Base = declarative_base()

class User(Base):
__tablename__ = 'users'
id = Column(Integer(), primary_key=True)
first_other: RelationshipProperty[Other] = relationship('Other')
second_other: RelationshipProperty[Iterable[Other]] = relationship(Other, uselist=True)
second_other: RelationshipProperty[List[Other]] = relationship(Other, uselist=True)
third_other: RelationshipProperty[Other] = relationship(Other, uselist=False)
bad_other: RelationshipProperty[Other] = relationship('Other', uselist=True) # E: Incompatible types in assignment (expression has type "RelationshipProperty[Iterable[Other]]", variable has type "RelationshipProperty[Other]")
bad_other: RelationshipProperty[Other] = relationship('Other', uselist=True) # E: Incompatible types in assignment (expression has type "RelationshipProperty[List[Other]]", variable has type "RelationshipProperty[Other]")

class Other(Base):
__tablename__ = 'other'
Expand Down