This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: `_remove_dups_flatten` in `typing.py` contains dead branch
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: kj, sobolevn
Priority: normal Keywords: patch

Created on 2022-01-22 10:16 by sobolevn, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30780 merged sobolevn, 2022-01-22 10:18
Messages (2)
msg411244 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-22 10:16
Here's how `_remove_dups_flatten` is defined right now:

```
def _remove_dups_flatten(parameters):
    """An internal helper for Union creation and substitution: flatten Unions
    among parameters, then remove duplicates.
    """
    # Flatten out Union[Union[...], ...].
    params = []
    for p in parameters:
        if isinstance(p, (_UnionGenericAlias, types.UnionType)):
            params.extend(p.__args__)
        elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
            params.extend(p[1:])
        else:
            params.append(p)

    return tuple(_deduplicate(params))
```

Source: https://github.com/python/cpython/blob/38afeb1a336f0451c0db86df567ef726f49f6438/Lib/typing.py#L274

It is only used in `def Union():`, source: https://github.com/python/cpython/blob/38afeb1a336f0451c0db86df567ef726f49f6438/Lib/typing.py#L522-L523

```
parameters = tuple(_type_check(p, msg) for p in parameters)
parameters = _remove_dups_flatten(parameters)
```

But, notice that `_remove_dups_flatten` contains this branch: `elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:`.

It is never executed, removing it does not change `test_typing` / `test_types` results. And it is reasonable: `_type_check` ensures that `parameters` can only contain types, not `tuple`s.

Proof:

```
>>> from typing import Union, get_type_hints

>>> Union[int, (Union, str, bool)]
# TypeError: Union[arg, ...]: each arg must be a type. Got (typing.Union, <class 'str'>, <class 'bool'>).

>>> class Some:
...    x: 'Union[int, (Union, str, bool)]'
... 
>>> get_type_hints(Some)
# TypeError: Union[arg, ...]: each arg must be a type. Got (typing.Union, <class 'str'>, <class 'bool'>).
```

Since it is pretty old, I guess the internal API has changed significantly and it is not needed anymore.

I am going to send a PR to remove it.
msg411483 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2022-01-24 15:43
New changeset c144d9363107b50bcb0ccd01e7202e26a40c21f0 by Nikita Sobolev in branch 'main':
bpo-46470: remove unused branch from `typing._remove_dups_flatten` (GH-30780)
https://github.com/python/cpython/commit/c144d9363107b50bcb0ccd01e7202e26a40c21f0
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90628
2022-01-24 16:10:16kjsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2022-01-24 15:43:03kjsetnosy: + kj
messages: + msg411483
2022-01-22 10:18:27sobolevnsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28965
2022-01-22 10:16:13sobolevncreate