pyright icon indicating copy to clipboard operation
pyright copied to clipboard

Magic calls to deprecated __getitem__ not flagged as deprecated

Open SamB opened this issue 3 months ago • 1 comments

Describe the bug

Calls to __getitem__ resulting from indexing syntax don't report deprecation warnings when they should.

Acording to https://typing.python.org/en/latest/spec/directives.html#deprecated:

  • Any syntax that indirectly triggers a call to the function. For example, if the __add__ method of a class C is deprecated, then the code C() + C() should trigger a diagnostic. Similarly, if the setter of a property is marked deprecated, attempts to set the property should trigger a diagnostic.

This principle seems equally applicable here.

Code or Screenshots

Code sample in pyright playground

from warnings import deprecated


class Foo:
    @deprecated("Never did anything useful")
    def __getitem__(self, key: str):
        pass

x = Foo()
x['bar'] # Should warn but doesn't
x.__getitem__('baz') # Warns as expected

VS Code extension or command-line

I haven't tried it locally using upstream pyright or pylance; pyright-play.net says this is 1.1.406.

SamB avatar Oct 01 '25 17:10 SamB

In general, implicit use of magic method calls are not flagged as deprecated. I've added support for a handful of them, but most are not supported. Surfacing them requires significant work for each magic method call.

Changing this to an enhancement request.

erictraut avatar Oct 01 '25 18:10 erictraut