fix(typing): improve accuracy of type propagation for collections #2724
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related to #2695. It is a short change, but I want to explain the "why" clearly for future reference.
Recent studying of python type system and playing around the codebase made me realize a couple of things that are still wrong with the existing type propagation:
StringFieldsCollectionis a actually recognized by type checkers as a collection of class objects rather than class instances. For example, this is whystring_field.get_entity_data()below is expecting two arguments (the instance and the index) instead of one (the index):In summary, turns out:
subtypeparameter ofcollection_factorymethod as what actually drives the type parameterization of that method. This is solved by using a type parameter specific to the method. See here.subtypewithType[Type Param]rather than justType Param. Doing that ensuresCollection[Type Param]in the return annotation is correctly interpreted as a collection of instances and not of class objects.With this change, things work accurately now across both PyCharm and vscode.