Mypy: --disallow-any-generics generates spurious messages

Created on 27 Jun 2019  路  6Comments  路  Source: python/mypy

In certain cases, --disallow-any-generics will generate an error message in the file where a type alias is used with the line number at which the type alias is defined.

For example

[case testDisallowAnyGenericsMessages]
# mypy: disallow-any-generics
from a import B
x: B

[file a.py]
from typing import TypeVar, List

T = TypeVar('T')

A = List[T]
B = A

[builtins fixtures/list.pyi]

generates the error main:6: error: Missing type parameters for generic type. main:6 is not a line that appears.

The error here is line 6 of a.py, B = A, though that is not an error in this case because disallow-any-generics is not specified for that file. It is triggered by line 3 of main, but reporting an error there would also be wrong because there is nothing wrong with line 3 of main, just with the definition of B. (And there is nothing that can be fixed at that call site, because B is not generic!).

I'm marking this as high priority because disallow-any-generics is a super important option that we I think we should make a default once it is fixed.

bug priority-0-high

Most helpful comment

You probably need to put Test[Any] in your bound. This example looks rather different from the original one in this bug.

All 6 comments

In general it seems to me we should prohibit using types as contexts for errors in general (or make file name part of error context).

I think that types are probably fine as error contexts in the semantic analyzer.

Sorry, I have problem.

from typing import TypeVar, Generic, Any

T = TypeVar('T', bound='Test')
class Test(Generic[T]):
    def output(self):
        print("out")

test.py:3: error: Missing type parameters for generic type

I can't understand this error.

I also have same problem with @happy2mylife .

Is this problem not solved yet? @msullivan

You probably need to put Test[Any] in your bound. This example looks rather different from the original one in this bug.

Was this page helpful?
0 / 5 - 0 ratings