Skip to content

Tuple[AnyStr] always get converted into Tuple[unicode] in Py2 #6761

Closed
@gsnedders

Description

@gsnedders
  • Are you reporting a bug, or opening a feature request?
    A bug.

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

from typing import AnyStr, Union, Text, Tuple


def anystr_func(s):
    # type: (AnyStr) -> AnyStr
    return s

def anystr_tuple_func(s):
    # type: (AnyStr) -> Tuple[AnyStr]
    return (s,)

class Foo(object):
    def __init__(self, s):
        # type: (AnyStr) -> None
        reveal_type(s)
        self.s = s  # type: Union[bytes, Text]
        reveal_type(self.s)
        self.t = anystr_func(s)  # type: Union[bytes, Text]
        reveal_type(self.t)
        self.u = anystr_tuple_func(s)  # type: Tuple[Union[bytes, Text]]
        reveal_type(self.u)
  • What is the actual behavior/output?
foo.py:15: error: Revealed type is 'builtins.str*'
foo.py:15: error: Revealed type is 'builtins.unicode*'
foo.py:17: error: Revealed type is 'builtins.str'
foo.py:17: error: Revealed type is 'builtins.unicode'
foo.py:19: error: Revealed type is 'builtins.str'
foo.py:19: error: Revealed type is 'builtins.unicode'
foo.py:21: error: Revealed type is 'Tuple[builtins.unicode]'
  • What is the behavior/output you expect?
foo.py:15: error: Revealed type is 'builtins.str*'
foo.py:15: error: Revealed type is 'builtins.unicode*'
foo.py:17: error: Revealed type is 'builtins.str'
foo.py:17: error: Revealed type is 'builtins.unicode'
foo.py:19: error: Revealed type is 'builtins.str'
foo.py:19: error: Revealed type is 'builtins.unicode'
foo.py:21: error: Revealed type is 'Tuple[builtins.str]'
foo.py:21: error: Revealed type is 'Tuple[builtins.unicode]'
  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

Python 3.7.3, mypy 0.701

Yes (as of b29a433)

  • What are the mypy flags you are using? (For example --strict-optional)

--py2. Note I get the expected output with both str and bytes without --py2.


To be slightly more concrete, this came about trying to figure out what was going on with code calling os.path.split (which is (AnyStr) -> Tuple[AnyStr, AnyStr]), and not getting the results I was expecting.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions