Re: Feature Request: Adding Way to Annotate Class Variables Distinct from Instance Variables
Christopher Barker <[email protected]> Wed, 21 Dec 2022 08:58:10 -0800
| Newsgroups | gmane.comp.python.devel |
|---|---|
| Message-ID | <CALn7ch-1J-ECMk45xfK=BjCi2nRzP8LUs4y5gCdNoCJALnVN0A@mail.gmail.com> |
--===============5858812416992306892== Content-Type: multipart/alternative; boundary="0000000000000cfbd305f05976f4" --0000000000000cfbd305f05976f4 Content-Type: text/plain; charset="UTF-8" First: this is Python-dev, which is not really the best palce for this kind of question. I'd try: https://discuss.python.org/ Though interestingly, I don't see a Typing topic --maybe I missed it. Or this list: https://mail.python.org/archives/list/[email protected]/ But a couple thoughts: 1) I'm a bit confused -- you haven't done a type annotation for the class variable, only for the method argument. So not sure what you really intend here. 2) isinstance() checks run-time types -- it has nothing to do with type annotations. So I'm not totally clear on what you want here. But from your description, it sounds like you want to annotate the type of a class attribute and and an instance attribute with two different types. I have no idea if that's possible, but it does seem tobe be a "bad idea" -- and contrary to the goal of static typing. Annotating the type of a class attribute is setting it for instance attributes as well -- and that is intended behavior. HTH, -CHB On Wed, Dec 21, 2022 at 8:17 AM <[email protected]> wrote: > Hello folks, I am Chihiro, a.k.a. Frodo821, and this is my first post to > this group. > > I searched for a way to annotate both class variables and instance > variables with different types and concluded that there is currently no way > to do this. > > For example, what I want to: > ``` > class Foo: > variable_both_class_and_instance = Field(desc="descriptive string") > > def __init__(self, value: int): > self.variable_both_class_and_instance = value > > assert isinstance(Foo.variable_both_class_and_instance, Field) > assert isinstance(Foo().variable_both_class_and_instance, int) > ``` > > In this example, I want to annotate `Foo.variable_both_class_and_instance` > with `Field` when it is accessed as a class variable. On the other hand, I > want to annotate `Foo.variable_both_class_and_instance` with `int` when it > is accessed as an instance variable. > > I don't have any smart ideas to accomplish this, but I think > `typing.ClassVar` could be extended this like: > > ``` > class Foo: > variable_both_class_and_instance: Union[ClassVar[Field], int] = > Field(desc="descriptive string") > > def __init__(self, value: int): > self.variable_both_class_and_instance = value > > assert isinstance(Foo.variable_both_class_and_instance, Field) > assert isinstance(Foo().variable_both_class_and_instance, int) > ``` > > Do you have any ideas or comments about this? > _______________________________________________ > Python-Dev mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/python-dev.python.org/ > Message archived at > https://mail.python.org/archives/list/[email protected]/message/7XFIE6YGRGO3XKCR7MZGDN6CCGUNN6MR/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython --0000000000000cfbd305f05976f4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div>First: this is Python-dev, which is not really the be= st palce for this kind of question. I'd try:</div><div><br></div><div><= a href=3D"https://discuss.python.org/">https://discuss.python.org/</a></div= ><div><br></div><div>Though interestingly, I don't see a Typing topic -= -maybe I missed it.=C2=A0</div><div><br></div><div>Or this list:</div><div>= <a href=3D"https://mail.python.org/archives/list/[email protected]/">ht= tps://mail.python.org/archives/list/[email protected]/</a><br></div><di= v><br></div><div><br></div>But a couple thoughts:<div><br></div><div>1) I&#= 39;m a bit confused -- you haven't done a type annotation for the class= variable, only for the method argument. So not sure what you really intend= here.</div><div><br></div><div>2) isinstance() checks run-time types -- it= has nothing to do with type annotations.</div><div><br></div><div>So I'= ;m not=C2=A0totally clear on what you want here.</div><div><br></div><div>B= ut from your description, it sounds like you want to annotate=C2=A0the type= of a class attribute and and=C2=A0an instance attribute with two different= types. I have no idea if that's possible, but it does seem tobe be a &= quot;bad idea" -- and contrary to the goal of static typing.</div><div= ><br></div><div>Annotating=C2=A0the type of a class attribute is setting it= for instance attributes as well -- and that is intended behavior.</div><di= v><br></div><div>HTH,</div><div><br></div><div>-CHB</div><div><br></div><di= v>=C2=A0</div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class= =3D"gmail_attr">On Wed, Dec 21, 2022 at 8:17 AM <<a href=3D"mailto:sakai= [email protected]">[email protected]</a>> wrote:<br></div><blockquote c= lass=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px soli= d rgb(204,204,204);padding-left:1ex">Hello folks, I am Chihiro, a.k.a. Frod= o821, and this is my first post to this group.<br> <br> I searched for a way to annotate both class variables and instance variable= s with different types and concluded that there is currently no way to do t= his.<br> <br> For example, what I want to:<br> ```<br> class Foo:<br> =C2=A0 =C2=A0 variable_both_class_and_instance =3D Field(desc=3D"descr= iptive string")<br> <br> =C2=A0 =C2=A0 def __init__(self, value: int):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.variable_both_class_and_instance =3D value= <br> <br> assert isinstance(Foo.variable_both_class_and_instance, Field)<br> assert isinstance(Foo().variable_both_class_and_instance, int)<br> ```<br> <br> In this example, I want to annotate `Foo.variable_both_class_and_instance` = with `Field` when it is accessed as a class variable. On the other hand, I = want to annotate `Foo.variable_both_class_and_instance` with `int` when it = is accessed as an instance variable.<br> <br> I don't have any smart ideas to accomplish this, but I think `typing.Cl= assVar` could be extended this like:<br> <br> ```<br> class Foo:<br> =C2=A0 =C2=A0 variable_both_class_and_instance: Union[ClassVar[Field], int]= =3D Field(desc=3D"descriptive string")<br> <br> =C2=A0 =C2=A0 def __init__(self, value: int):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.variable_both_class_and_instance =3D value= <br> <br> assert isinstance(Foo.variable_both_class_and_instance, Field)<br> assert isinstance(Foo().variable_both_class_and_instance, int)<br> ```<br> <br> Do you have any ideas or comments about this?<br> _______________________________________________<br> Python-Dev mailing list -- <a href=3D"mailto:[email protected]" target= =3D"_blank">[email protected]</a><br> To unsubscribe send an email to <a href=3D"mailto:[email protected]= rg" target=3D"_blank">[email protected]</a><br> <a href=3D"https://mail.python.org/mailman3/lists/python-dev.python.org/" r= el=3D"noreferrer" target=3D"_blank">https://mail.python.org/mailman3/lists/= python-dev.python.org/</a><br> Message archived at <a href=3D"https://mail.python.org/archives/list/python= [email protected]/message/7XFIE6YGRGO3XKCR7MZGDN6CCGUNN6MR/" rel=3D"noreferre= r" target=3D"_blank">https://mail.python.org/archives/list/python-dev@pytho= n.org/message/7XFIE6YGRGO3XKCR7MZGDN6CCGUNN6MR/</a><br> Code of Conduct: <a href=3D"http://python.org/psf/codeofconduct/" rel=3D"no= referrer" target=3D"_blank">http://python.org/psf/codeofconduct/</a><br> </blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"= class=3D"gmail_signature"><div dir=3D"ltr">Christopher Barker, PhD (Chris)= <br><br> Python Language Consulting<br>=C2=A0 - Teaching<br>=C2=A0 - Scient= ific Software Development<br>=C2=A0 - Desktop GUI and Web Development<br>= =C2=A0 - wxPython, numpy, scipy, Cython<br></div></div> --0000000000000cfbd305f05976f4-- --===============5858812416992306892== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline