[New-bugs-announce] [issue46603] `typing._strip_annotations` is not fully covered

Nikita Sobolev report at bugs.python.org
Tue Feb 1 16:17:41 EST 2022


New submission from Nikita Sobolev <mail at sobolevn.me>:

Right now `coverage` says that this line is not covered at all: https://github.com/python/cpython/blob/bebaa95fd0f44babf8b6bcffd8f2908c73ca259e/Lib/typing.py#L1882

Considering how hard all these `types.UnionType` / `typing.Union` stuff is and that the logic with `reduce` and `operator.or_` is also quite complex, I think it is important to cover it.

It actually took me some time to reach this line, but here's the test I came up with:

```
    def test_get_type_hints_annotated_in_union(self):
        def with_union(x: int | list[Annotated[str, 'meta']]): ...

        self.assertEqual(get_type_hints(with_union), {'x': int | list[str]})
        self.assertEqual(
            get_type_hints(with_union, include_extras=True),
            {'x': int | list[Annotated[str, 'meta']]},
        )
```

Note that direct `|` with `Annotated` does not work, because it triggers `_AnnotatedType.__or__`, which returns `typing.Union` and not `types.UnionType`.

I will send a PR with it in a minute :)

Any feedback is welcome!

----------
components: Library (Lib)
messages: 412308
nosy: AlexWaygood, gvanrossum, kj, sobolevn
priority: normal
severity: normal
status: open
title: `typing._strip_annotations` is not fully covered
type: behavior
versions: Python 3.10, Python 3.11, Python 3.9

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


More information about the New-bugs-announce mailing list