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

Fix Index.query and Index.scan typing issues #748

Merged
merged 1 commit into from Jan 25, 2020
Merged

Conversation

ikonst
Copy link
Contributor

@ikonst ikonst commented Jan 24, 2020

This now works again:

from pynamodb.attributes import NumberAttribute
from pynamodb.models import Model
from pynamodb.indexes import GlobalSecondaryIndex
from pynamodb.pagination import ResultIterator

class UntypedIndex(GlobalSecondaryIndex):
    bar = NumberAttribute(hash_key=True)

class MyModel(Model):
    foo = NumberAttribute(hash_key=True)
    bar = NumberAttribute()

    untyped_index = UntypedIndex()

# Ensure old code keeps working
untyped_result: ResultIterator = MyModel.untyped_index.query(123)
model: MyModel = next(untyped_result)
not_model: int = next(untyped_result)  # this is legacy behavior so it's "fine"

and then this is also possible now:

class TypedIndex(GlobalSecondaryIndex[MyModel]):
    #                                ^^^^^^^^^
    bar = NumberAttribute(hash_key=True)

class MyModel(Model):
    foo = NumberAttribute(hash_key=True)
    bar = NumberAttribute()

    typed_index = TypedIndex()

typed_result: ResultIterator[MyModel] = MyModel.typed_index.query(123)
my_model: MyModel = next(typed_result)
not_model: int = next(typed_result)  # E: Incompatible types in assignment (expression has type "MyModel", variable has type "int")

Ditto for scan.

@ikonst ikonst marked this pull request as ready for review January 24, 2020 22:03
Copy link
Member

@garrettheel garrettheel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Nice work and thanks for the quick fix!

@ikonst ikonst merged commit 5426ed9 into master Jan 25, 2020
@garrettheel garrettheel deleted the index-typing-workaround branch January 25, 2020 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants