Skip to content

LocalProxy breaking isinstance() for abc.ABC #2188

@douglas-raillard-arm

Description

@douglas-raillard-arm

When tested with isinstance() against an ABC, LocalProxy instances raise an exception rather than returning False.

import abc
from werkzeug import local

ls=local.LocalStack()
x=ls()

class C(abc.ABC):
	pass

isinstance(x, C)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/abc.py", line 119, in __instancecheck__
    return _abc_instancecheck(cls, instance)
  File "/usr/lib/python3.9/abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

The call to isinstance() should have returned False rather than an exception.

It seems to be related to the fact that x.__class__ is not a class as expected, but an unbound method. Calling it with x.__class__() returns the expected werkzeug.local.LocalProxy class. While it's not documented in Python except for builtin types, most of the ecosystem will assume type(x) is x.__class__, including it appears the standard library.

Environment:

  • Python version: Python 3.9.6
  • Werkzeug version: 2.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions