Well, the first comment is that this isn't really the best list to ask such
questions on, since it was created for the Python developers to discuss the
development of the language and its implementation. Further, such
discussions nowadays take place on discuss.python.org, and you can find
more information at https://www.python.org/community/lists/.

However, I simplified your program a little, and would observe that the
following program raises no AssertionErrors under Python 3.10  and 3.11.

class Foo:
    variable_both_class_and_instance: str | int = "descriptive string"

    def __init__(self, value: int):
        assert isinstance(self.variable_both_class_and_instance, str)
        self.variable_both_class_and_instance = value
        assert isinstance(self.variable_both_class_and_instance, int)

assert isinstance(Foo.variable_both_class_and_instance, str)
assert isinstance(Foo(42).variable_both_class_and_instance, int)

Union is now outdated, since we can use alternation (|) to offer
alternative types.

It's therefore not obvious to me from your email why ClassVar would need
any modification, or even why it needs to be used in your example. Perhaps
I've missed your point?

Kind regards,
Steve


On Wed, Dec 21, 2022 at 4:15 PM <sakaic2...@gmail.com> 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 -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/7XFIE6YGRGO3XKCR7MZGDN6CCGUNN6MR/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/MD37VH7ZWB4PZVQ4ZJY7IIIU7DMGFQC5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to