Skip to content

runtime checkable protocols potential raising error with custom getattribute #105134

Closed as not planned
@hmc-cs-mdrissi

Description

@hmc-cs-mdrissi

Bug report

I'm unsure if this is more tensorflow bug or python bug but it does trace to this pr and error message traces to cpython source. I've also commented on issue in tensorflow side here.

# typing 3.9/3.10 produces no error, typing_extensions will trigger it.
from typing_extensions import Protocol, runtime_checkable

from tensorflow.python.trackable.data_structures import _DictWrapper


@runtime_checkable
class Foo(Protocol):
    def bar(self) -> None:
        ...


isinstance(_DictWrapper(), Foo)

With typing extensions an error message is produced of,

Traceback (most recent call last):
  File "/Users/pa-loaner/Snapchat/Dev/training-platform/scratch/runtime_protocol_custom_dict.py", line 12, in <module>
    isinstance(_DictWrapper(), Foo)
  File "/Users/pa-loaner/Snapchat/Dev/.venvs/tf212/lib/python3.9/site-packages/typing_extensions.py", line 604, in __instancecheck__
    val = inspect.getattr_static(instance, attr)
  File "/Users/pa-loaner/.pyenv/versions/3.9.16/lib/python3.9/inspect.py", line 1711, in getattr_static
    instance_result = _check_instance(obj, attr)
  File "/Users/pa-loaner/.pyenv/versions/3.9.16/lib/python3.9/inspect.py", line 1654, in _check_instance
    instance_dict = object.__getattribute__(obj, "__dict__")
TypeError: this __dict__ descriptor does not support '_DictWrapper' objects

Your environment

I've tested on cpython 3.9.16 and 3.10.10 where using typing works. I'd suspect 3.11 will work as well. Using typing_extensions 4.6 triggers error with inspect.getattr_static usage. Reported here instead of typing extensions as adding getattr static there was backport from here. I'd expect this to reproduce on 3.12 although installing tf on 3.12 is likely tricky and easier to test with typing extensions. The _DictWrapper class is here and is like a subclass of __dict__ with a custom getattribute to do some extra tracking of elements.

I used latest version of tensorflow (2.12). The underlying error message comes from this line. As this can be triggered in attribute access unsure whether it should be TypeError vs AttributeError. If it was attribute error getattr static would catch it and be fine. If it needs to be type error then it's unclear to me whether tensorflow class is breaking some assumption or if getattr static should also catch type errors here. In particular it's interesting that object.__getattribute__ can raise TypeError and inspect.getattr_static can fail.

I'll try to look tomorrow if I can make a more self contained example that doesn't require tensorflow.

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13bugs and security fixesstdlibPython modules in the Lib dirtopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions