[New-bugs-announce] [issue43355] __future__.annotations breaks inspect.signature()

Eric Engestrom report at bugs.python.org
Mon Mar 1 11:02:00 EST 2021


New submission from Eric Engestrom <eric.engestrom at gmail.com>:

We have a pytest that boils down to the following:

```
#from __future__ import annotations
from inspect import Parameter, signature


def foo(x: str) -> str:
    return x + x


def test_foo():
    expected = (
        Parameter("x", Parameter.POSITIONAL_OR_KEYWORD, annotation=str),
    )

    actual = tuple(signature(foo).parameters.values())

    assert expected == actual
```

(execute with `pip install pytest && pytest -vv test_foo.py`)

I tried importing 3.10 annotations (so that we can get rid of quotes around the class containing `foo()`, which is omitted here because it isn't necessary to reproduce the bug), but doing so changes the output of `inspect.signature()` but not the output `inspect.Parameter()`, causing a mismatch between the two that breaks the test.

The above passes on 3.7.9, 3.8.7 & 3.9.1, and if I uncomment the first line, it fails on those same versions.
As can be expected, the annotations import is a no-op on 3.10.0a5 and the test passes either way.

I expect `inspect` might have not been correctly updated to support postponed annotations, but I haven't looked at the implementation (I'm not familiar with the CPython codebase at all) so it's just a guess.

----------
components: Library (Lib)
messages: 387875
nosy: 1ace
priority: normal
severity: normal
status: open
title: __future__.annotations breaks inspect.signature()
type: behavior
versions: Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43355>
_______________________________________


More information about the New-bugs-announce mailing list