Skip to content

"Missing type parameters for generic type" gives incorrect results for type aliases #6128

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

Closed
mgilson opened this issue Jan 2, 2019 · 2 comments

Comments

@mgilson
Copy link
Contributor

mgilson commented Jan 2, 2019

I'm trying mypy==0.650 on my codebase (using --strict) and I'm hitting some new positives associated with --disallow-any-generics. Some of these are legit warnings that I should fix, but a couple have me flummoxed.

For example, the following is an attempt to describe the type that you can pass to requests as the json= argument (it'll take a file-like object, anything that you can shove into json.dumps a string or bytes object):

from typing import Union, Sequence, Any, IO, AnyStr, Mapping

# This is a pretty lousy approximate way to say "an object that can be made into JSON"
_JSON_TYPE_ISH = Union[
    Mapping[str, Any],
    Sequence[Any],
    str, int, None]
_REQUEST_BYTES = Union[
    IO[AnyStr],
    AnyStr, None]
_REQUEST_TYPE = Union[
    _JSON_TYPE_ISH,
    _REQUEST_BYTES,
    None
]

Running this gets me:

$ mypy --strict sandbox/foo.py
sandbox/foo.py:13: error: Missing type parameters for generic type

However, if I inline the _REQUEST_BYTES type alias, it seems to work OK:

from typing import Union, Sequence, Any, IO, AnyStr, Mapping

# This is a pretty lousy approximate way to say "an object that can be made into JSON"
_JSON_TYPE_ISH = Union[
    Mapping[str, Any],
    Sequence[Any],
    str, int, None]
# _REQUEST_BYTES = Union[
#     IO[AnyStr],
#     AnyStr, None]
_REQUEST_TYPE = Union[
    _JSON_TYPE_ISH,
    # _REQUEST_BYTES,
    Union[IO[AnyStr], AnyStr, None],
    None
]

It's possible that this issue is related to #6112 -- though I tried to read though that issue and I'm not entirely sure if it's just another symptom of the same problem or not.

@AvdN
Copy link

AvdN commented Jan 3, 2019

If I read ilevkivskyi comment on issue #6112 correctly, you should be able to get rid of the error by changing line 13 to read: _REQUEST_BYTES[AnyStr]. I am not sure if that is the correct solution, but

# This is a pretty lousy approximate way to say "an object that can be made into JSON"
_JSON_TYPE_ISH = Union[
    Mapping[str, Any],
    Sequence[Any],
    str, int, None]
_REQUEST_BYTES = Union[
    IO[AnyStr],
    AnyStr, None]
_REQUEST_TYPE = Union[
    _JSON_TYPE_ISH,
    _REQUEST_BYTES[AnyStr],
    None
]

doesn't give an error.

@ilevkivskyi
Copy link
Member

@mgilson This is not a bug. You define a generic alias and then use it without a type parameter (FYI AnyStr is a type variable).

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