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

runtime_checkable error in newer versions caused by a protocol subclass #207

Closed
vnmabus opened this issue May 31, 2023 · 6 comments · Fixed by #208
Closed

runtime_checkable error in newer versions caused by a protocol subclass #207

vnmabus opened this issue May 31, 2023 · 6 comments · Fixed by #208
Assignees
Labels
bug Something isn't working

Comments

@vnmabus
Copy link

vnmabus commented May 31, 2023

Consider the following code:

from typing import Sized

isinstance(1, Sized)

This works well and returns False, as expected.

However, if I create a new protocol that subclass Sized:

from typing_extensions import Protocol

class MySized(Protocol, Sized):
    pass

isinstance(1, Sized)

This now raises the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-2516c5c4af5f> in <module>
----> 1 isinstance(1, Sized)

.../lib/python3.8/typing.py in __instancecheck__(self, obj)
    764 
    765     def __instancecheck__(self, obj):
--> 766         return self.__subclasscheck__(type(obj))
    767 
    768     def __subclasscheck__(self, cls):

.../lib/python3.8/typing.py in __subclasscheck__(self, cls)
    769         if self._special:
    770             if not isinstance(cls, _GenericAlias):
--> 771                 return issubclass(cls, self.__origin__)
    772             if cls._special:
    773                 return issubclass(cls.__origin__, self.__origin__)

.../lib/python3.8/abc.py in __subclasscheck__(cls, subclass)
    100         def __subclasscheck__(cls, subclass):
    101             """Override for issubclass(subclass, cls)."""
--> 102             return _abc_subclasscheck(cls, subclass)
    103 
    104         def _dump_registry(cls, file=None):

.../lib/python3.8/site-packages/typing_extensions.py in __subclasscheck__(cls, other)
    581                     "Protocols with non-method members don't support issubclass()"
    582                 )
--> 583             return super().__subclasscheck__(other)
    584 
    585         def __instancecheck__(cls, instance):

.../lib/python3.8/abc.py in __subclasscheck__(cls, subclass)
    100         def __subclasscheck__(cls, subclass):
    101             """Override for issubclass(subclass, cls)."""
--> 102             return _abc_subclasscheck(cls, subclass)
    103 
    104         def _dump_registry(cls, file=None):

.../lib/python3.8/site-packages/typing_extensions.py in _proto_hook(cls, other)
    637             if _allow_reckless_class_checks():
    638                 return NotImplemented
--> 639             raise TypeError("Instance and class checks can only be used with"
    640                             " @runtime_checkable protocols")
    641 

TypeError: Instance and class checks can only be used with @runtime_checkable protocols

This non-local behavior did not happen in prior versions, and has caused problems in my code (GAA-UAM/scikit-fda#540).

@AlexWaygood

This comment was marked as off-topic.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2023
@vnmabus
Copy link
Author

vnmabus commented May 31, 2023

I am NOT using isinstance with MySized, please check the code again.

@AlexWaygood AlexWaygood reopened this May 31, 2023
@AlexWaygood
Copy link
Member

AlexWaygood commented May 31, 2023

Yikes, that's not good! Thanks for the report. Apologies for misreading your example.

@jgbos
Copy link

jgbos commented Jun 2, 2023

@AlexWaygood I'm having similar issues. One thing I noticed in my issue you might look at, if you run debug on the error is the error thrown with cls == Sized or on cls == MySized. I noticed the TypeError was thrown on any of protocol without runtime_checkable.

@AlexWaygood
Copy link
Member

@jgbos are you still experiencing this issue with the latest version of typing_extensions (4.6.3)?

@jgbos
Copy link

jgbos commented Jun 2, 2023

@AlexWaygood I just saw your other comment, checking now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants