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

Detect unsupported use of self types in generic classes #8020

Closed
JukkaL opened this issue Nov 27, 2019 · 0 comments · Fixed by #8021
Closed

Detect unsupported use of self types in generic classes #8020

JukkaL opened this issue Nov 27, 2019 · 0 comments · Fixed by #8021
Labels
bug mypy got something wrong needs discussion

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 27, 2019

The last line generates a strange error message:

from typing import TypeVar, Generic, Type

M = TypeVar("M")
T = TypeVar("T")

class Descriptor(Generic[M]):
    pass

class BaseWrapper(Generic[M]):
    @classmethod
    def create_wrapper(cls, metric_descriptor):
        # type: (Type[T], Descriptor[M]) -> T
        raise NotImplementedError
    
class WrapperInstance(BaseWrapper[M]):
    @classmethod
    def create_wrapper(cls, descriptor):
        # type: (Descriptor[M]) -> WrapperInstance[M]
        return WrapperInstance()
    
def build_wrapper(descriptor):
    # type: (Descriptor[M]) -> BaseWrapper[M]
    wrapper = WrapperInstance  # type: Type[BaseWrapper[M]]
    # Argument 1 to "create_wrapper" of "BaseWrapper" has incompatible type "Descriptor[M]"; 
    # expected "Descriptor[BaseWrapper[M]]"
    return wrapper.create_wrapper(descriptor)

The root cause seems to be the signature of BaseWrapper.create_wrapper; it shouldn't use two type variables M and T. It would be better to generate an error about the signature instead of the call site. However, there may be something deeper going on.

@JukkaL JukkaL added bug mypy got something wrong needs discussion labels Nov 27, 2019
ilevkivskyi added a commit that referenced this issue Nov 27, 2019
Fixes #8020

There is a bunch of code/logic duplication around `bind_self()`, mostly because of #7724. This PR makes all three main code paths consistently follow the same structure:
1. `freshen_function_type_vars()`
2. `bind_self()`
3. `expand_type_by_instance(..., map_instance_to_supertype())` (a.k.a `map_type_from_supertype()`)

I briefly scrolled through other code paths, and it looks like this was last major/obvious inconsistency (although code around `__getattr__`/`__setattr__`/`__get__`/`__set__` looks a bit suspicious).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong needs discussion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant