Skip to content

Conversation

@pradeep90
Copy link
Contributor

Thanks to Eric Traut who tested all the code examples in Pyright and
raised implementation concerns about Self type in aliases.

I ran into implementation issues in Pyre too. This is because Pyre
preprocesses things like Self in an early phase and does alias
resolution in a later phase. Mixing the two would lead to a lot of
hacks.

@JelleZijlstra I believe you had some concerns about using Tuple this way at runtime. Do we need to include them too?

@Gobot1234 What do you think? Do you have any workarounds or strong objections?

Here is our prior discussion about aliases on the Google Doc. A user had brought up a concern there too that Self outside a class would be confusing. Given that Eric pushed back too, I feel we can drop this.

Copy-pasting the discussion for easy reference:

Sergei Lebedev
12:44 PM Oct 25
Is it valid to use Self in a type alias?

Gobot1234
1:12 PM Oct 25
Like `alias: TypeAlias = list[Self]` at a top level?

Sergei Lebedev
1:22 PM Oct 25
Yep, or even alias = Self or alias = list[Self] without the TypeAlias annotation.

Gobot1234
1:32 PM Oct 25
Yeah I think both of those should be fine

Sergei Lebedev
1:40 PM Oct 25
I can see how a type checker could support both, but I'm not sure if the PEP should allow that, though, unless we could find a convincing use-case. Wdyt?

Gobot1234
3:07 PM Oct 25
I somewhat agree but I don't really see why allowing apart from Self being sort of unbound and being potentially difficult to implement it would be so bad, some people come up with monstrous types so it might be helpful there and for that use case I'd probably consider it worthwhile.

Sergei Lebedev
8:24 AM Oct 26
Sure, my point was that seeing Self outside of a class body could look confusing, since there is no enclosing class. On a slightly unrelated note, how would a "local" type alias like

class A:
T = Self
x: T

work?

Gobot1234
9:08 AM Oct 26
reveal_type(A().x) revealed type would be Self@__main__.A I would imagine.

Pradeep Kumar Srinivasan
1:09 PM Oct 26
@Sergei Both should be fine, but as you said this can be hard to understand. I've added a simple example.

If there's any pushback from the rest of the community or the other type checkers, we'll drop it since it isn't crucial.

Thanks to Eric Traut who tested all the code examples in Pyright and
raised implementation concerns about ``Self`` type in aliases.

I ran into implementation issues in Pyre too. This is because Pyre
preprocesses things like ``Self`` in an early phase and does alias
resolution in a later phase. Mixing the two would lead to a lot of
hacks.
@JelleZijlstra
Copy link
Member

I'm fine with this change.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, makes sense.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants