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
This came up in #3569. See also the later comments in python/typing#11 for background. Mapping is not a protocol (as opposed to most other typing ABCs). This mean code like the following will not work:
Deriving MyMap from Mapping[str, int] works, though. I suggest we don't allow new submissions to use Mapping as argument type and work towards removing existing uses. Instead they should use a custom protocol, which can also be narrower than Mapping.
@srittau I am trying to create a Protocol in PR#3877 but when I use Protocol[_KT, _VT_co] it says Invariant type variable '_KT' used in protocol where contravariant one is expected, can't understand what could be causing this.
Rather than disallowing Mapping as an argument type because Mapping is not a protocol, is it better to instead make Mapping a protocol? That seems like it would result in better usability than the alternative of forbidding Mapping arguments. Worth writing down the pros and cons of both options before picking which one? Related protocols (SupportsGetItem, etc.) were recently added in #4325 since this issue was created. Does that change things?
Relatedly, if we had python/mypy#2922, I guess Mapping.register(MyMap) could be added to the example in the issue description to resolve the error, but mypy does not yet support ABCMeta.register.
I think it's hard to make Mapping a protocol, since it's an abstract base class in collections.abc, and since it's so widely used. But maybe it would make sense to introduce a separate MappingProto (or similar) protocol and use that going forward. Although, using more specific protocols is still more useful, but harder to implement.
Indeed. We looked into this and found it would be very difficult. I strongly recommend just inheriting from Mapping over coming up with yet another duck typing protocol. FWIW mypy probably will never support .register().
We have _typeshed.SupportsKeysAndGetItem now, and use it quite widely. It's a very useful protocol.
Possibly we could look into adding lints to detect bad uses of Mapping in argument annotations. The issue tracker at flake8-pyi would probably be a better place to discuss that, though.
Activity
djb4ai commentedon Mar 26, 2020
@srittau I am trying to create a Protocol in PR#3877 but when I use
Protocol[_KT, _VT_co]it saysInvariant type variable '_KT' used in protocol where contravariant one is expected, can't understand what could be causing this.Introduce Mapping-like protocols
Introduce Mapping-like protocols (#4325)
jab commentedon Aug 12, 2020
Rather than disallowing
Mappingas an argument type becauseMappingis not a protocol, is it better to instead makeMappinga protocol? That seems like it would result in better usability than the alternative of forbiddingMappingarguments. Worth writing down the pros and cons of both options before picking which one? Related protocols (SupportsGetItem, etc.) were recently added in #4325 since this issue was created. Does that change things?Relatedly, if we had python/mypy#2922, I guess
Mapping.register(MyMap)could be added to the example in the issue description to resolve the error, but mypy does not yet supportABCMeta.register.srittau commentedon Aug 12, 2020
I think it's hard to make
Mappinga protocol, since it's an abstract base class incollections.abc, and since it's so widely used. But maybe it would make sense to introduce a separateMappingProto(or similar) protocol and use that going forward. Although, using more specific protocols is still more useful, but harder to implement.gvanrossum commentedon Aug 12, 2020
Indeed. We looked into this and found it would be very difficult. I strongly recommend just inheriting from Mapping over coming up with yet another duck typing protocol. FWIW mypy probably will never support .register().
AlexWaygood commentedon Nov 1, 2023
We have
_typeshed.SupportsKeysAndGetItemnow, and use it quite widely. It's a very useful protocol.Possibly we could look into adding lints to detect bad uses of
Mappingin argument annotations. The issue tracker at flake8-pyi would probably be a better place to discuss that, though.