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

Failing to use variables for types #3962

Open
mitar opened this Issue Sep 17, 2017 · 6 comments

Comments

Projects
None yet
3 participants
@mitar

mitar commented Sep 17, 2017

If I have file foo.py:

from typing import *

A = TypeVar('A')

class Foo(Generic[A]):
    def foo(self) -> A:
        pass

    def bar(self) -> A:
        pass

The following file bar.py raises strange errors:

from foo import Foo

import numpy as np  # type: ignore

A = np.ndarray

class Bar(Foo[A]):
    def foo(self) -> A:
        pass

    def bar(self) -> A:
        pass

Errors:

bar.py:7: error: Invalid type "bar.A"
bar.py:8: error: Invalid type "bar.A"
bar.py:11: error: Invalid type "bar.A"

If I inline A then it works:

from foo import Foo

import numpy as np  # type: ignore

class Bar(Foo[np.ndarray]):
    def foo(self) -> np.ndarray:
        pass

    def bar(self) -> np.ndarray:
        pass

Python 3.5.2
mypy 0.521

@gvanrossum

This comment has been minimized.

Show comment
Hide comment
@gvanrossum

gvanrossum Sep 17, 2017

Member

This comes from this line:

self.fail('Invalid type "{}"'.format(name), t)

A simple one-file repro:

from typing import *
T = TypeVar('T')
class Foo(Generic[T]): pass
import blah  # type: ignore
B = blah
class Bar(Foo[B]): pass

I think there's something wrong with the block before (the comment # Something with an Any type sounds like someone was a bit worried when writing this code).

(UPDATE: I think it's kind of bad that there are so many places in the code that just say "Invalid type" -- it would be nice if the errors were all different so it's possible to trace them down easier, and perhaps also give the user a clue about what mypy thinks is the matter.)

Member

gvanrossum commented Sep 17, 2017

This comes from this line:

self.fail('Invalid type "{}"'.format(name), t)

A simple one-file repro:

from typing import *
T = TypeVar('T')
class Foo(Generic[T]): pass
import blah  # type: ignore
B = blah
class Bar(Foo[B]): pass

I think there's something wrong with the block before (the comment # Something with an Any type sounds like someone was a bit worried when writing this code).

(UPDATE: I think it's kind of bad that there are so many places in the code that just say "Invalid type" -- it would be nice if the errors were all different so it's possible to trace them down easier, and perhaps also give the user a clue about what mypy thinks is the matter.)

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Sep 17, 2017

This also does not work with the same error:

from typing import *
T = TypeVar('T')
class Foo(Generic[T]): pass
B = None
class Bar(Foo[B]): pass

mitar commented Sep 17, 2017

This also does not work with the same error:

from typing import *
T = TypeVar('T')
class Foo(Generic[T]): pass
B = None
class Bar(Foo[B]): pass
@ilevkivskyi

This comment has been minimized.

Show comment
Hide comment
@ilevkivskyi

ilevkivskyi Sep 17, 2017

Collaborator

@mitar Concerning aliases to None: the PEP 484 rule that None is equivalent to type(None) only applies to type context, not to runtime context, to create an alias one can write Void = type(None), see #3754

Concerning the original error, we can in principle allow creating aliases to unimported types (since we now have TypeOfAny.unimported). Anyway, the error message is clearly too terse.

Collaborator

ilevkivskyi commented Sep 17, 2017

@mitar Concerning aliases to None: the PEP 484 rule that None is equivalent to type(None) only applies to type context, not to runtime context, to create an alias one can write Void = type(None), see #3754

Concerning the original error, we can in principle allow creating aliases to unimported types (since we now have TypeOfAny.unimported). Anyway, the error message is clearly too terse.

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Sep 17, 2017

The following does not work either:

from typing import *
T = TypeVar('T')
class Foo(Generic[T]): pass
B = type(None)
class Bar(Foo[B]): pass

I get foo.py:5: error: Invalid type "foo.B".

mitar commented Sep 17, 2017

The following does not work either:

from typing import *
T = TypeVar('T')
class Foo(Generic[T]): pass
B = type(None)
class Bar(Foo[B]): pass

I get foo.py:5: error: Invalid type "foo.B".

@ilevkivskyi

This comment has been minimized.

Show comment
Hide comment
@ilevkivskyi

ilevkivskyi Sep 17, 2017

Collaborator

@mitar

The following does not work either:

Yes, since PR referenced by me is not yet merged, next time please read references before replying.

Collaborator

ilevkivskyi commented Sep 17, 2017

@mitar

The following does not work either:

Yes, since PR referenced by me is not yet merged, next time please read references before replying.

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Sep 17, 2017

I missed the fact that is is not yet merged. Sorry.

mitar commented Sep 17, 2017

I missed the fact that is is not yet merged. Sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment