-
- Notifications
You must be signed in to change notification settings - Fork 339
Retrofit assert_type for Aggregate and Callable #935
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
base: develop
Are you sure you want to change the base?
Retrofit assert_type for Aggregate and Callable #935
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've only worked on aggregate.py and callable.py, but found some areas that need quick-fixings.
I think it'd make more sense to include these quick-fixes in the same PR, as the tests are supposed to check exactly the behaviors these quick-fixes are supposed to affect.
@ZipFile let me know what you think.
| | ||
| | ||
| provider1_new_non_string_keys: providers.Aggregate[str] = providers.Aggregate( | ||
| # TODO: Change providers.Aggregate to accept Mapping? Then remove explicit typing here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current stub:
class Aggregate(Provider[T]): def __init__( self, provider_dict: Optional[_Dict[Any, Provider[T]]] = None, **provider_kwargs: Provider[T], ): ...I think we should accept collections.abc.Mapping here
class Aggregate(Provider[T]): def __init__( self, provider_dict: Optional[Mapping[Any, Provider[T]]] = None, **provider_kwargs: Provider[T], ): ...Because Dict is invariant in both key and value, so providers.Aggregate cannot infer its own typevar without explicit typing
provider1_new_non_string_keys = providers.Aggregate( {Cat: providers.Object("str")}, ) # This raises mypy error| args4 = provider4.args | ||
| kwargs4 = provider4.kwargs | ||
| assert_type(args4, Tuple[Any]) | ||
| # TODO: Change Callable.kwargs to Dict[str, Any]? Then adjust test back to Dict[str, Any] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The stub actually declares provider4.kwargs to return Dict[Any, Any], not Dict[str, Any] like the previous test probably intended to check.
But I think we can safely change it to return Dict[str, Any] in the stub.
| attr_getter5 = provider5.provided.attr | ||
| item_getter5 = provider5.provided["item"] | ||
| method_caller5 = provider5.provided.method.call(123, arg=324) | ||
| # TODO: Remove explicit typing of Provider.provided return type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The stub is this
@property def provided(self) -> ProvidedInstance[T]: ...But ProvidedInstance is not declared to be generic
class ProvidedInstance(Provider, ProvidedInstanceFluentInterface): def __init__(self, provides: Optional[Provider] = None) -> None: ...I think we can make the quick fix to
@property def provided(self) -> ProvidedInstance: ...| # Test 10: to check the .provides | ||
| provider10 = providers.Callable(Cat) | ||
| provides10: Optional[Callable[..., Cat]] = provider10.provides | ||
| assert provides10 is Cat |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these assert statements are not doing anything
| | ||
| # Test 12: to check string imports | ||
| provider12: providers.Callable[Dict[Any, Any]] = providers.Callable("builtins.dict") | ||
| # TODO: Use T_Any as Callable typevar? Then remove type-ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't infer typevar without explicit typing, mypy raises error here, but we can change it to typevar with default Any to fix it
Partially addresses #934