CarliJoy / intersection_examples

Python Typing Intersection examples
MIT License
33 stars 2 forks source link

Combination with `Self` #23

Closed junkmd closed 1 year ago

junkmd commented 1 year ago

While reading specification.rst, I had a question that crossed my mind.

In below case, unless from __future__ import annotations is used, a NameError is raised at runtime.

https://github.com/CarliJoy/intersection_examples/blob/bcca49112164a9a8b0cdfb94569bb379c734b722/specification.rst?plain=1#L89-L98

To avoid runtime errors, it's necessary to use string literals for typing as shown below.

    class LoginRequiredMixin: 
        def dispatch(self: "LoginRequiredMixin & View", request, *args, **kwargs): ... 

For expressing that self is a mixin, in order to convey the same meaning without using string literals, I think that it should be typed with using Self.

    class LoginRequiredMixin: 
        def dispatch(self: Self & View, request, *args, **kwargs): ... 

Moreover, it's necessary to consider the interaction with methods and attributes that return Self.

class Mixin:
    me: Self

a: Mixin & T
…
b = a.me

In the above case, I think that b should be interpreted as Mixin & T by type checkers.

Any opinions would be appreciated.

CarliJoy commented 1 year ago

Feel free to create an MR

junkmd commented 1 year ago

@CarliJoy

Hello!

I see, I will start working for the first pull request in this repo.

junkmd commented 1 year ago

24 is merged.