On Fri, Nov 26, 2021 at 1:37 AM Paul Moore <p.f.mo...@gmail.com> wrote:

> On Thu, 25 Nov 2021 at 21:45, Christopher Barker <python...@gmail.com>
> wrote:
> > The issue is that, intended or not, typing is making it's way into
> Python culture. As an instructor of beginning python users, I am unsure at
> this point when to introduce type annotations.
> >
> > What is their role? Up to today, I have treated them as an advanced
> feature, useful for "complex codebases". But there are any number of
> examples springing up on the internet, to the point where many students now
> think they are "best practice", if not actually required.
>
> Agreed this is somewhat OT, but I also think the messaging around
> annotations needs to be reviewed. I suspect that students will also
> not clearly understand the fact that annotations aren't checked at
> runtime. And IMO this is particularly difficult to get across in the
> case of non-typechecker uses of annotations.
>
> The following, for example, is extremely non-intuitive to me:
>
> >>> from dataclasses import dataclass
> >>>
> >>> @dataclass
> ... class A:
> ...   n: int
> ...
> >>> a = A("didn't expect this!")
> >>> a
> A(n="didn't expect this!")
>
> Even though the "typing is optional" message is well-understood, my
> instincts still lead me to expect that declaring the type of n will
> result in a runtime type-check, in the constructor at least. (If I
> check the code with mypy, it does raise an error, so that's good.
> Although the more I think about it, given that I believe dataclasses
> use eval "under the hood", the less I understand *how* it manages to
> do that without special-case knowledge of the dataclass decorator...)
>

Static checkers special-case the @dataclass decorator. Eric Traut has a
proposal to generalize this support (sorry, I'm in a rush, otherwise I'd
dig up the link, but it's in the typing-sig archives).


> I'd like to see a clearer statement from "somewhere" about how APIs
> should use annotations at runtime, such that Python users have a much
> clearer intuition about APIs like the dataclass one, and library
> designers can build their APIs based on a clear "common understanding"
> of what to expect when annotations are used.
>

Note that @dataclass itself is very careful not to use the annotations, it
only looks for their *presence*. With one exception for ClassVar.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
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/CEGCZKRLE62T4PUMGGLZ7YOFH33LQ2KM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to