You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mypy indicates that the type of inspect.signature(some_method).parameters.keys() is typing.AbstractSet[_KT], while the type of collections.OrderedDict().keys() is collections._OrderedDictKeysView[typing.Any]. However, their runtime types match exactly.
As a result of (1), inspect.signature(some_method).parameters.keys() doesn't like being hinted with typing.KeysView.
Actual behaviour: to reproduce
importtypingfrominspectimportsignaturefromcollectionsimportOrderedDict# OKdictKeys: typing.KeysView= {}.keys()
# OKdictValues: typing.ValuesView= {}.values()
# mypy: Incompatible types in assignment (expression has type "AbstractSet[str]", variable has type "KeysView[Any]")mappingproxyKeys: typing.KeysView=signature(object.__init__).parameters.keys()
# OKmappingProxyValues: typing.ValuesView=signature(object.__init__).parameters.values()
# OKorderedDictKeys=OrderedDict({1: 3}).keys()
# mypy: Revealed type is "collections._OrderedDictKeysView[Any]"reveal_type(orderedDictKeys)
# runtime: No `AssertionError`; types match exactlyasserttype(orderedDictKeys) istype(mappingproxyKeys)
Expected Behavior
# No `mypy` errors here expectedmappingproxyKeys: typing.KeysView=signature(object.__init__).parameters.keys()
Your Environment
Mypy version used: 0.910
Mypy command-line flags: None
Mypy configuration options from mypy.ini (and other config files): None
Python version used: 3.8
Operating system and version: Ubuntu 20.04 LTS
The text was updated successfully, but these errors were encountered:
Pardon my ignorance, but why should the return types of Dict (and other Mappings) not be changed (akin to #3473)? Alternatively, why have ItemsView or KeysView at all if "implementors" of Mapping aren't going to use them? I've probably missed something important, but ItemsView and KeysView seem like the more permissive of the two. Why not converge on those everywhere?
I guess another way to ask all those questions is, would your suggestion address this?
# test_case.pyfromtypingimportAbstractSet, Dict, ItemsView, Mapping, Tuple, TypedDictclassOneTwo(TypedDict):
one: inttwo: intot: OneTwo= {"one": 1, "two": 2}
d: Dict[str, object] =dict(ot)
ot_map: Mapping[str, object] =ot# No error. A lie of omission?d_map: Mapping[str, object] =d# Another?ot_items: AbstractSet[Tuple[str, object]] =ot.items() # This worksd_items: AbstractSet[Tuple[str, object]] =d.items() # So does thisot_items2: ItemsView[str, object] =ot.items() # And thisd_items2: ItemsView[str, object] =d.items() # This tooot_items=ot_map.items() # All goodd_items=d_map.items() # So is this# These both generate errors: Incompatible types in assignment (expression has type# "AbstractSet[Tuple[str, object]]", variable has type "ItemsView[str, object]"ot_items2=ot_map.items()
d_items2=d_map.items()
This is probably a stupid question, but why shouldn't an ItemView[str, object] be assignable to something of type AbstractSet[Tuple[str, object]] if AbstractSet[Tuple[str, object]] is narrower?
Bug Report
Possibly related to #3473.
mypy
indicates that the type ofinspect.signature(some_method).parameters.keys()
istyping.AbstractSet[_KT]
, while the type ofcollections.OrderedDict().keys()
iscollections._OrderedDictKeysView[typing.Any]
. However, their runtime types match exactly.inspect.signature(some_method).parameters.keys()
doesn't like being hinted withtyping.KeysView
.Actual behaviour: to reproduce
Expected Behavior
Your Environment
0.910
mypy.ini
(and other config files): None3.8
Ubuntu 20.04 LTS
The text was updated successfully, but these errors were encountered: