python / python/mypy

stubtest and @deprecated

Open
#20,160 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-pep-702 topic-stubtest
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

The @warnings.deprecated decorator adds a few dunders to classes it decorates. This causes stubtest to complain about differences between stub and implementation, unless these changes are added manually to the stubs. I think we should special case @deprecated to ignore these differences.

To Reproduce

For example, adding the following test to StubtestUnit:

    @collect_cases
    def test_deprecated(self) -> Iterator[Case]:
        yield Case(
            stub="""
            from warnings import deprecated

            @deprecated("")
            class Foo: ...
            """,
            runtime="""
            from warnings import deprecated

            @deprecated("")
            class Foo:
                pass
            """,
            error=None,
        )

causes the following error when running the tests with Python 3.13:

E           AssertionError: error: test_module.Foo.__deprecated__ is not present in stub
E             Stub: in file test_module.pyi
E             MISSING
E             Runtime:
E             ''
E             
E             error: test_module.Foo.__init_subclass__ is not present in stub
E             Stub: in file test_module.pyi
E             MISSING
E             Runtime: in file /opt/pyenv/versions/3.13.8/lib/python3.13/warnings.py:621
E             def (*args, **kwargs)
E             
E             error: test_module.Foo.__new__ is not present in stub
E             Stub: in file test_module.pyi
E             MISSING
E             Runtime: in file /opt/pyenv/versions/3.13.8/lib/python3.13/warnings.py
E             def (*args, **kwargs)
E             
E             Found 3 errors (checked 1 module)
E             
E           assert {'test_module....Foo.__new__'} == set()
E             
E             Extra items in the left set:
E             'test_module.Foo.__deprecated__'
E             'test_module.Foo.__new__'
E             'test_module.Foo.__init_subclass__'
E             Use -v to get more diff

Interestingly, this is different from what we see in typeshed (python/typeshed#14893):

error: jwcrypto.jwt.JWTMissingKeyID.__init_subclass__ is inconsistent, stub does not have *args parameter "args"
Stub: in file /home/runner/work/typeshed/typeshed/stubs/jwcrypto/jwcrypto/jwt.pyi:139
def (cls: type[builtins.object])
Runtime: in file /opt/hostedtoolcache/Python/3.13.8/x64/lib/python3.13/warnings.py:621
def (*args, **kwargs)

Your Environment

  • Mypy version used: master for the test above, 1.18.2 in typeshed CI
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.13

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proposed test in StubtestUnit and reproduce it on Python 3.13. Trace stubtest’s comparison of classes decorated with warnings.deprecated, then make the test pass while ignoring only the decorator-added differences.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.