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

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

Closed
RafikBellahsene-it opened this issue May 24, 2023 · 10 comments

Comments

@RafikBellahsene-it
Copy link

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)

@AlexWaygood
Copy link
Member

AlexWaygood commented May 24, 2023

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')
```

@jgbos
Copy link

jgbos commented May 25, 2023

I'm still trying to get a small reproducible example for you, but the issue I'm seeing is the same TypeError above. It is thrown on some other type I've defined and is not related to the object that is undergoing the issubclass check. That is, in @RafikBellahsene-it error message cls would not be the object I'd expect, but some other Protocol object I've defined but does not have a @runtime_checkable decorator.

Hope that makes sense, I've been failing at making a small reproducible example but will keep trying.

@RafikBellahsene-it if you run pdb is cls at "577" the object you'd expect?

@AlexWaygood
Copy link
Member

If you're calling isinstance() or issubclass() against a Protocol that isn't decorated with @runtime_checkable, then it's correct for typing_extensions to 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 typing module on Python 3.10+, so it was a bug in typing_extensions that we weren't consistently raising an exception in these situations.

The change to typing_extensions.Protocol was made in #132, and was documented in our changelog.

@elena
Copy link

elena commented May 25, 2023

Hey we're getting same, suspect it's a conflict with FastAPI/pydantic stack 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 class

Happy to make new ticket if appropriate.

@AlexWaygood
Copy link
Member

AlexWaygood commented May 26, 2023

@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.

@elena
Copy link

elena commented May 26, 2023

@AlexWaygood tyty

@jgbos
Copy link

jgbos commented May 27, 2023

@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 pdb I check what cls in the error message is, it is not my_type but rather some_other_type in typing.py that does not have the decorator. I find that weird as I was not doing an issubclass on some_other_type.

That's all the info I have now though. Hopefully early next week I can have a small example.

@JelleZijlstra
Copy link
Member

Perhaps this was the same as #207?

@AlexWaygood
Copy link
Member

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?

@AlexWaygood
Copy link
Member

This 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! :)

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants