Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_remove_dups_flatten in typing.py contains dead branch #90628

Closed
sobolevn opened this issue Jan 22, 2022 · 2 comments
Closed

_remove_dups_flatten in typing.py contains dead branch #90628

sobolevn opened this issue Jan 22, 2022 · 2 comments
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

BPO 46470
Nosy @sobolevn, @Fidget-Spinner
PRs
  • bpo-46470: remove unused branch from typing._remove_dups_flatten #30780
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2022-01-24.16:10:16.872>
    created_at = <Date 2022-01-22.10:16:13.368>
    labels = ['type-bug', 'library', '3.11']
    title = '`_remove_dups_flatten` in `typing.py` contains dead branch'
    updated_at = <Date 2022-01-24.16:10:16.872>
    user = 'https://github.com/sobolevn'

    bugs.python.org fields:

    activity = <Date 2022-01-24.16:10:16.872>
    actor = 'kj'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-24.16:10:16.872>
    closer = 'kj'
    components = ['Library (Lib)']
    creation = <Date 2022-01-22.10:16:13.368>
    creator = 'sobolevn'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46470
    keywords = ['patch']
    message_count = 2.0
    messages = ['411244', '411483']
    nosy_count = 2.0
    nosy_names = ['sobolevn', 'kj']
    pr_nums = ['30780']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46470'
    versions = ['Python 3.11']

    @sobolevn
    Copy link
    Member Author

    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:

    def _remove_dups_flatten(parameters):

    It is only used in `def Union():`, source:

    cpython/Lib/typing.py

    Lines 522 to 523 in 38afeb1

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

    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 tuples.

    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.

    @sobolevn sobolevn added 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 22, 2022
    @Fidget-Spinner
    Copy link
    Member

    New changeset c144d93 by Nikita Sobolev in branch 'main':
    bpo-46470: remove unused branch from typing._remove_dups_flatten (GH-30780)
    c144d93

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants