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

Type inference issue with "map" and "os.path.normcase" #8961

Closed
deveshks opened this issue Jun 7, 2020 · 5 comments
Closed

Type inference issue with "map" and "os.path.normcase" #8961

deveshks opened this issue Jun 7, 2020 · 5 comments

Comments

@deveshks
Copy link

deveshks commented Jun 7, 2020

The following code raises a type error when run with mypy v0.780.

import os

files = ['foo/BAR', 'FOO/baz']
normcased_files = map(os.path.normcase, files)

The errors are as follows

$ mypy --version
mypy 0.780

$ mypy scratch.py 

scratch.py:4: error: Argument 1 to "map" has incompatible type overloaded function; expected "Callable[[str], AnyStr]"
Found 1 error in 1 file (checked 1 source file)

$ mypy --py2  scratch.py 

scratch.py:4: error: Argument 1 to "map" has incompatible type "Callable[[AnyStr], AnyStr]"; expected "Callable[[str], AnyStr]"
Found 1 error in 1 file (checked 1 source file)

Note that the error is different when run against python 2 vs python 3, even when the type annotations are same for both python 3 and python 2

@Kangaroux
Copy link

VScode correctly identifies the sig when you hover: normpath(path: str) -> str

This works:

import os

def fn(s: str) -> str:
    return os.path.normpath(s)

files = ['foo/BAR', 'FOO/baz']
normcased_files = map(fn, files)


reveal_type(os.path.normpath)
# note: Revealed type is 'Overload(def [AnyStr in (builtins.str, builtins.bytes)] (path: builtins._PathLike[AnyStr`-1]) -> AnyStr`-1, def [AnyStr in (builtins.str, builtins.bytes)] (path: AnyStr`-1) -> AnyStr`-1)'

reveal_type(fn)
# note: Revealed type is 'def (x: builtins.str) -> builtins.str'

@Kangaroux
Copy link

You can do this as a one line workaround:

normcased_files = map(lambda x: os.path.normpath(x), files)

And if you care about the type:

from typing import List
normcased_files: List[str] = list(map(lambda x: os.path.normpath(x), files))

@Kangaroux
Copy link

With regards to the comment on #8962, since the map function is using overloads, did you try mypy 0.770 to see if that works?

@deveshks
Copy link
Author

deveshks commented Jun 8, 2020

You can do this as a one line workaround:

Thanks. I was aware of the workaround, but that's not the actual problem here. The original code should also pass type checks.

With regards to the comment on #8962, since the map function is using overloads, did you try mypy 0.770 to see if that works?

No, that doesn't work either

$ mypy --version
mypy 0.770

$ mypy scratch.py 
scratch.py:4: error: Argument 1 to "map" has incompatible type overloaded function; expected "Callable[[str], AnyStr]"

@AlexWaygood
Copy link
Member

Closing as a duplicate of #2389 (although the example is slightly different, the bug appears to be to be the same)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants