Repository navigation
TypeError: Instance and class checks can only be used with @runtime_checkable protocols #187
Description
Activity
Hi @RafikBellahsene-it! Would you be able to provide us with a minimal, reproducible example for this exception? Some instructions here: https://stackoverflow.com/help/minimal-reproducible-example. Without it, it's hard for us to figure out exactly what might be causing this exception.
Please also put triple-backticks around your code, like this, to ensure that GitHub formats it correctly:
``` # This is my code sample def foo(x): if x > 0: print('hi') ```I'm still trying to get a small reproducible example for you, but the issue I'm seeing is the same
TypeErrorabove. It is thrown on some other type I've defined and is not related to the object that is undergoing theissubclasscheck. That is, in @RafikBellahsene-it error messageclswould not be the object I'd expect, but some otherProtocolobject I've defined but does not have a@runtime_checkabledecorator.Hope that makes sense, I've been failing at making a small reproducible example but will keep trying.
@RafikBellahsene-it if you run
pdbisclsat "577" the object you'd expect?If you're calling
isinstance()orissubclass()against aProtocolthat isn't decorated with@runtime_checkable, then it's correct fortyping_extensionsto raise an exception. If a protocol isn't decorated with@runtime_checkable, you're not meant to be able to check it at runtime.Previously, there were situations where we previously failed to consistently raise an exception. This didn't match the behaviour of the
typingmodule on Python 3.10+, so it was a bug intyping_extensionsthat we weren't consistently raising an exception in these situations.The change to
typing_extensions.Protocolwas made in #132, and was documented in our changelog.Hey we're getting same, suspect it's a conflict with
FastAPI/pydanticstack but haven't yet gotten together a simple example (still managing pinning typing-extensions to an earlier version).File "pydantic/main.py", line 205, in pydantic.main.ModelMetaclass.__new__ File "pydantic/fields.py", line 491, in pydantic.fields.ModelField.infer File "pydantic/fields.py", line 421, in pydantic.fields.ModelField.__init__ File "pydantic/fields.py", line 537, in pydantic.fields.ModelField.prepare File "pydantic/fields.py", line 641, in pydantic.fields.ModelField._type_analysis File "/usr/lib64/python3.8/typing.py", line 774, in __subclasscheck__ return issubclass(cls, self.__origin__) TypeError: issubclass() arg 1 must be a classHappy to make new ticket if appropriate.
@elena, that's a separate error message and a separate issue, which has already been reported and fixed. Please see pydantic/pydantic#5821 (and #179). You should be able to fix it by pinning pydantic>=1.10.8.
Reacted by Elena Williams@AlexWaygood tyty
Reacted by Alex Waygood@AlexWaygood Sorry, I've been busy. Still haven't gotten you a reproducible example. I understand when I need to add
@runtime_checkable, but I need to understand the updates that have been made. The basic problem is this:- I have protocols defined in
typing.py, some have the decorator and some do not - I do a
issubclass(my_type, Protocol)and get the error above - When I run with
pdbI check whatclsin the error message is, it is notmy_typebut rathersome_other_typeintyping.pythat does not have the decorator. I find that weird as I was not doing anissubclassonsome_other_type.
That's all the info I have now though. Hopefully early next week I can have a small example.
- I have protocols defined in
Perhaps this was the same as #207?
Perhaps this was the same as #207?
I was just about to comment the same thing. @RafikBellahsene-it or @jgbos, can you check to see if the problem still occurs with
typing_extensions==4.6.3?Reacted by Justin GoodwinThis has been open a week now, and nobody has been able to provide a reproducible example, which means there's nothing we can do to help. Hopefully this was the same issue as #207, in which case it's now been fixed in version 4.6.3. If anybody still has a problem with the latest version of typing_extensions, and is able to provide a way for us to reproduce the problem, please open a new issue! :)
Hello I am facing this issue since this morning. The code was working good, it stopped to work suddenly
dataframe = dataframe.withColumn(joiningColumn[0],decode(col(joiningColumn[0]),'UTF-8'))
149 .withColumn(joiningColumn[1],decode(col(joiningColumn[1]),'UTF-8'))
151 warning_df = GreatExpectationsAnalyser(self.spark,dataframe).convertToGEObject()
152 .expect_column_values_to_be_in_set(joiningColumn[0],
--> 153 list(dataframe.where(col(joiningColumn[1]).isNotNull()).select(joiningColumn[1]).distinct().toPandas()[joiningColumn[1]]),
The exception is raised by
File /databricks/python/lib/python3.9/site-packages/typing_extensions.py:584, in _ProtocolMeta.subclasscheck(cls, other)
576 if (
577 getattr(cls, '_is_protocol', False)
578 and not cls.callable_proto_members_only
579 and not _allow_reckless_class_checks(depth=3)
580 ):
581 raise TypeError(
582 "Protocols with non-method members don't support issubclass()"
583 )
--> 584 return super().subclasscheck(other)