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
_______________________________________________ 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/SAYAELPZXMBQCJNDMPLNKVD3PA2FGG53/ Code of Conduct: http://python.org/psf/codeofconduct/
