r/Python • u/genericlemon24 • Nov 11 '21
News PEP 673 -- Self Type
https://www.python.org/dev/peps/pep-0673/56
u/Floozutter Nov 11 '21 edited Nov 11 '21
As someone who appreciates static type checking and occasionally likes adding alternate "constructors" to classes using classmethod, this is super convenient! No need to define a TypeVar with a bound for correctness:
from typing import Self class Shape: @classmethod def from_config(cls, config: dict[str, float]) -> Self: return cls(config["scale"])
It's also nice that this would sorta be a "canonical" typing for self in instance methods once accepted.
2
u/energybased Nov 12 '21
It's also nice that this would sorta be a "canonical" typing f
Yup, plenty of people are getting that wrong by annotating self as the enclosing class type.
12
u/henryschreineriii Nov 12 '21
The fact this is used 500+ times in typeshed should tell you it's needed! I've always used the TypeVar, and hated how I had to make one per class I wanted to annotate. And how many people just use the name of the class and incorrectly support subclassing. Any time you return self.__class__(...) this is useful.
7
u/aes110 Nov 11 '21
Never personally needed something like this but I can definitely see the need for it
8
Nov 12 '21
we really need a statically typed python dialect
2
u/energybased Nov 12 '21
Might as well get type annotations working first! Then just make them required if that's what you want.
2
1
u/xorvtec Nov 12 '21
I get it, but isn't "self" a variable name by convention? It doesn't mean anything implicitly. I guess I'm just picking nits at the "Self" name selection.
4
-1
u/Abitconfusde Nov 11 '21
I just turned on strict type checking in vs code and fuuhhh..... It looks like I cut my wrists on the screen. I've tried understanding the peps. Is there a good basic typing tutorial? FWIW I'm a newb working with Django...
-1
u/MarsupialMole Nov 11 '21
This is why type annotations suck unless you're working on a new module.
In Django you have validators and an ORM to cover your big integration points. Don't stress too much. Add types as you go to new code. When you understand it well you can choose to go back and update legacy code.
0
-7
u/Yoghurt42 Nov 11 '21
Not sure what I should think when the first example given is an anti-pattern (setter)
5
Nov 11 '21
I mean copy is pretty easy and valid example. The issue more or less comes down to do we need a more extreme solution (like the string one) or is the only real case that cannot be avoided. The other case where you need to reference a namespace before it is declared is usually the result of bad code structure. I think this solves the only real situation you need to have type annotation before the namespace exists.
-8
u/Yoghurt42 Nov 11 '21
I mean copy is pretty easy and valid example.
And the PEP makes no mention of that.
6
Nov 11 '21
Because it is already known to be needed. This isn't a discussion about the merits of being able to reference an object outside of known namespace ever we already know its needed given that they almost nukes pydantic for it. It is about does this solve the issue. You don't need great examples just ones that illustrate how its used. You are overthinking this.
-10
u/Yoghurt42 Nov 11 '21
A PEP is supposed to show how the proposed feature will make real world code better, be that easier to understand, or make something possible that was awkward before.
Therefore, if the author chooses an example that is an anti-pattern it makes me question the whole PEP.
For example, where Self can be useful is when writing "fluent interfaces", but the author didn't bother to look for some code where it might come handy. But the author's example would never pass code review. Which leaves a bad taste in my mouth, if he didn't bother to find a good example, why should I think that he put a lot of thought into the proposed change. Showing your audience that you put a lot of thought into it will make it much easier to convince them.
1
u/MegaIng Nov 12 '21
You are assuming that the author is acting either in bad faith or an idiot, qnd not even considering the easier solution that they just choose a smal example instead of one with to much content. Just showing 10 lines examples because they are more realistic distracts from the PEP.
0
u/Yoghurt42 Nov 12 '21 edited Nov 12 '21
They chose a horrible example. And also you came up with a better example pretty much instantly.
You are assuming that the author is acting either in bad faith or an idiot,
Don't put words in my mouth. I don't. I said
Not sure what I should think when the first example given is an anti-pattern
My whole point is: If the author doesn't take a bit of time to come up with a realistic example, I assume he didn't spend much time thinking about his proposal at all. PEPs are there to convince people that what you are suggesting is a good idea, and you need a good example for that. Don't let the reader having to spend some time thinking to find a valid use case. The author is doing himself and the PEP a disservice when the first example makes people want to stop reading because it's "clearly" not well thought out. The author makes some valid points later on, but probably lost quite a few readers in the beginning.
0
u/MegaIng Nov 12 '21
I assume he didn't spend much time thinking about his proposal at all.
That's just as bad as assuming he is an idiot.
This PEP is a summary. It doesn't show off the full power of the feature.
PEPs are there to convince people that what you are suggesting is a good idea
This is simply not true. Read PEP 1 It's supposed to be short technical summary of the feature, with an rational as a basis for discussion. Other PEPs for larger features might spend more time on the rational, but they still show toy examples (PEP-636 comes to mind).
1
u/Decency Nov 11 '21
Yep, dunno why examples always do shit like this. "Here's code you would never write, and here's how I can improve it with this new feature!"
7
Nov 12 '21
[removed] — view removed comment
3
u/alkasm github.com/alkasm Nov 12 '21
Or maybe Instance over Cls, since it doesn't return a class but an instance.
1
u/tunisia3507 Nov 12 '21
Cls is more in keeping with the rest of
typing, where you state the name of the class, unless you need the type itself, in which case you usetyping.Type[Cls](type[Cls]as of 3.9).1
1
u/energybased Nov 12 '21 edited Nov 12 '21
The convention for class methods is
clssoClswould make more sense.If you're arguing for the same meaning, but to call it
Clsinstead, then that would be confusing becauseclswould be annotatedtype[Cls]and self would be annotatedCls.If you're arguing for
Clsto annotate the first parameter of a class method, then that wouldn't work since you still needSelffor the returned object, and it would be confusing to have bothSelfandCls.Would be confusing if the function returns a DIFFERENT instance of the same class.
Values have nothing to do with type annotations, but I see why that might confuse some people.
1
u/Gobot1234 Nov 13 '21
Yeah so I've actually had this brought up before so when you type hint a copy method which returns a different instance of self
This is currently how you do this:
```py
FooT = TypeVar("FooT", bound="Foo")
class Foo:
def copy(self: FooT) -> FooT:
return self.__class__()
```
and we didn't want to change the semantics of this so with typing.Self this becomes
```
from typing import Self
class Foo:
def copy(self) -> Self:
return self.__class__()
```
Also I think this shows how the method doesn't have to return `cls()` and isn't exclusively for use in classmethods.
Along with this, the name comes from Rust which uses Self which has been pointed out in other parts of this post.I do also plan to talk to people working on the documentation at typing.rtfd.io to get a note about this.
33
u/genericlemon24 Nov 11 '21