[Python-Dev] Re: PEP 701 – Syntactic formalization of f-strings

2022-12-21 Thread Pablo Galindo Salgado
Hi everyone,

For those that are not following the discussion in the discourse thread, I
am holding a poll to get an idea of the community opinions on how quotes
should work in nested f-strings for PEP 701:

https://discuss.python.org/t/pep-701-syntactic-formalization-of-f-strings/22046/24

For us is very important to have everyone represented and every opinion
heard and taken into consideration so feel free to vote in the poll leaving
your opinion. But please, **read first the PEP and the previous
discussion**. This is very important to get the context and previous
arguments in favour and against the different options.

Thanks a lot for your help!

Regards from cloudy London,
Pablo Galindo Salgado

On Mon, 19 Dec 2022 at 17:59, Pablo Galindo Salgado 
wrote:

> Hi everyone,
>
> I am very excited to share with you a PEP that Batuhan Taskaya, Lysandros
> Nikolaou and myself have been working on recently: PEP 701 - PEP 701 –
> Syntactic formalization of f-strings .
>
> We believe this will be a great improvement in both the maintainability of
> CPython and the usability of f-strings.
>
> We look forward to hearing what you think about this and to get your
> feedback!
>
> *Here is a TLDR for your convenience:*
>
>- The PEP proposes a formalized grammar for f-strings in Python by
>adding f-strings directly into the Grammar instead of using a two-pass
>hand-written parser.
>- This would lift some existing restrictions for f-strings that (we
>believe) will improve the user experience with f-strings.
>- Other benefits include:
>   - Reduced maintenance costs for f-string parsing code as well as
>   improved usability for users and library developers.
>   - Better error messages involving f-strings by leveraging the PEG
>   parser machinery.
>   - The proposed changes would improve the overall consistency of the
>   language and provide a way for alternative implementations to accurately
>   implement f-strings.
>
> *** IMPORTANT: direct all discussions to the discussion thread on
> discourse: 
> https://discuss.python.org/t/pep-701-syntactic-formalization-of-f-strings/22046
> 
> ***
>
> Thanks a lot, everyone for your time!
> Regards from rainy London,
> Pablo Galindo Salgado
>
___
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/Q22YQBHTVRUFKRMAETL3CDR7JN3YRJAG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: Feature Request: Adding Way to Annotate Class Variables Distinct from Instance Variables

2022-12-21 Thread Christopher Barker
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/typing-...@python.org/


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  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/
>


-- 
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
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/SAYAELPZXMBQCJNDMPLNKVD3PA2FGG53/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: Feature Request: Adding Way to Annotate Class Variables Distinct from Instance Variables

2022-12-21 Thread Steve Holden
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  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/


[Python-Dev] Feature Request: Adding Way to Annotate Class Variables Distinct from Instance Variables

2022-12-21 Thread sakaic2003
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/