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

Providing type information to mixin classes #119

Closed
ivanrvpereira opened this issue May 9, 2019 · 4 comments
Closed

Providing type information to mixin classes #119

ivanrvpereira opened this issue May 9, 2019 · 4 comments
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request

Comments

@ivanrvpereira
Copy link

ivanrvpereira commented May 9, 2019

I don't know if this is a bug, but we should be able to provide type information to mixin classes.

I'm using a _Base class to provide type info to the static type checker.
This technique is not elegant but solves all typing problems within the class, but on Pyright (mypy does not complain) it rises another error showed on the screenshot below.

An example of a Django Serializer Mixin

if TYPE_CHECKING:
    _Base = GenericAPIView
else:
    _Base = object


class DetailSerializerMixin(_Base):
    """
    Add custom serializer for detail view
    """

    detail_serializer_class = None
    queryset_detail: Optional[QuerySet] = None

    def get_serializer_class(self):
        error_message = "'{0}' should include a 'detail_serializer_class' attribute".format(
            self.__class__.__name__
        )
        assert self.detail_serializer_class is not None, error_message
        if self._is_request_to_detail_endpoint():
            return self.detail_serializer_class
        else:
            return super().get_serializer_class()

The error:
Screenshot 2019-05-09 16 47 53

If this is not the best approach, can you help me find a solution to provide type info?

@erictraut
Copy link
Collaborator

The issue is that the symbol TYPE_CHECKING is treated specially by mypy, but pyright doesn't know about it. Out of curiosity, why are you conditionally deriving from GenericAPIView only in the type checking path rather than always deriving from it?

My philosophy with pyright is to always type check the code that will execute at runtime, not some alternative/conditional path that is specific to the type checker.

@ivanrvpereira
Copy link
Author

Because this is a mixin (https://en.wikipedia.org/wiki/Mixin) and I want to add this behavior to any class that supports it. It could be the GenericAPIView or any other that behaves like it.

There is a similar thread on mypy: python/mypy#5837

@erictraut
Copy link
Collaborator

That approach for mix-ins strikes me as very fragile and ill-advised, but I don't understand your particular use case well enough to suggest a better approach.

I've implemented special-case support for the TYPE_CHECKING to mirror the behavior in mypy. This will be in the next release of pyright.

@erictraut erictraut added enhancement request New feature or request addressed in next version Issue is fixed and will appear in next published version labels May 10, 2019
@erictraut
Copy link
Collaborator

Support for TYPE_CHECKING is now added in 1.0.26

heejaechang added a commit to heejaechang/pyright that referenced this issue Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants