Skip to content

Assignment to next in class body of Iterator elicits error (in PY2 mode) #2503

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
gvanrossum opened this issue Nov 28, 2016 · 9 comments
Closed
Labels
bug mypy got something wrong priority-0-high

Comments

@gvanrossum
Copy link
Member

Since #2380 was merged, the following example elicits an error when type-checked in Python 2 mode:

from typing import Iterator
class A: pass
class X(Iterator[A]):
  def __next__(self):
    # type: () -> A
    pass
  next = __next__

The error comes from the code added by #2380:

__tmp__.py: note: In class "X":
__tmp__.py:7: error: Incompatible types in assignment (expression has type Callable[[X], A], variable has type Callable[[Iterator[A]], A])
@JukkaL
Copy link
Collaborator

JukkaL commented Nov 28, 2016

The type of next in the class body should likely be inferred as Callable[[X], A] (i.e., the type of self should be derived from the enclosing class).

@gvanrossum
Copy link
Member Author

gvanrossum commented Nov 28, 2016 via email

@TrueBrain
Copy link
Contributor

Just to check if I understand the problem correctly, the following should also pass mypy not? (but it doesn't before the revert; same error as above)

T = TypeVar("T")

class B(Generic[T]):
        v = None  # type: T
        def n(self):
                # type: () -> T
                return self.v

class C(B[int]):
        def m(self):
                # type: () -> int
                return 1
        n = m

@gvanrossum
Copy link
Member Author

gvanrossum commented Nov 29, 2016 via email

@TrueBrain
Copy link
Contributor

Found an even more elegant example:

class B():
        def n(self, a: int) -> None: pass
class C(B):
        def m(self, a: int) -> None: pass
        n = m

Pushed a fix, but I would love some feedback if this is an okay way to go.

Additionally, would you be able to check if with this version your repo does validate successfully?

@gvanrossum
Copy link
Member Author

Yes, that example is better because it shows the problem is not due to generics.

Where's your fix? You're going to have to create a new PR.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 30, 2016

Both examples look good.

@gvanrossum
Copy link
Member Author

However here's a more complex example:

class I:
    def foo(self) -> None: pass

class A(I):
    def foo(self) -> None: pass

class B(A):
    def bar(self) -> None: pass
    foo = bar

class C(B):
    def bar(self) -> None: pass
    foo = bar

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 16, 2017

Note that this becomes relevant after #2510 is merged.

@JukkaL JukkaL added bug mypy got something wrong priority-0-high labels Jan 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-0-high
Projects
None yet
Development

No branches or pull requests

3 participants