Skip to content

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

Description

@RafikBellahsene-it

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)

Activity

  1. AlexWaygood commented on May 24, 2023

    @AlexWaygood
    Member

    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')
    ```
    
  2. jgbos commented on May 25, 2023

    @jgbos

    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?

  3. AlexWaygood commented on May 25, 2023

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

  4. elena commented on May 25, 2023

    @elena

    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.

  5. AlexWaygood commented on May 26, 2023

    @AlexWaygood
    Member

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

  6. elena commented on May 26, 2023

    @elena
  7. jgbos commented on May 27, 2023

    @jgbos

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

  8. JelleZijlstra commented on Jun 2, 2023

    @JelleZijlstra
    Member

    Perhaps this was the same as #207?

  9. AlexWaygood commented on Jun 2, 2023

    @AlexWaygood
    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?

  10. AlexWaygood commented on Jun 3, 2023

    @AlexWaygood
    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! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions