Hi typing-sig, PEP-563 has a "Backward compatibility" <https://www.python.org/dev/peps/pep-0563/#backwards-compatibility> section which contains examples of how type annotations work for nested classes. In particular, the PEP says that class C: class D: def method(self) -> D: ... should be accepted by the type checker. This does not contradict
Annotations using nested classes and their respective state are still valid. They can use local names or the fully qualified name.
but is still surprising, because if we apply the usual scoping rules, the name D is not visible from the body of D. The name C is visible, so is C.D, but D is not. Does this look right to you? If yes, could you help me understand the motivation for having this special-case in the PEP? As an aside, I have tried this example in mypy (0.800), PyCharm (2020.3), pyright (in pylance 2021.6.2) and pytype (2021.05.25), and none of them seem to accept it. The same goes for typing.get_type_hints, which fails with a NameError Python 3.9.2 (default, Feb 28 2021, 17:03:44)
from __future__ import annotations import typing class C: ... class D: ... def method(self) -> D: ... ... typing.get_type_hints(C.D.method) Traceback (most recent call last): ... NameError: name 'D' is not defined
Cheers, Sergei
You're right, this violates the standard scoping rules. I suspect it is a mistake by the PEP author. I'm adding Lukasz to the discussion, hopefully he remembers why the PEP says this. On Sat, Jun 19, 2021 at 12:10 AM Sergei Lebedev <sergei.a.lebedev@gmail.com> wrote:
Hi typing-sig,
PEP-563 has a "Backward compatibility" <https://www.python.org/dev/peps/pep-0563/#backwards-compatibility> section which contains examples of how type annotations work for nested classes. In particular, the PEP says that
class C: class D: def method(self) -> D: ...
should be accepted by the type checker. This does not contradict
Annotations using nested classes and their respective state are still valid. They can use local names or the fully qualified name.
but is still surprising, because if we apply the usual scoping rules, the name D is not visible from the body of D. The name C is visible, so is C.D, but D is not.
Does this look right to you? If yes, could you help me understand the motivation for having this special-case in the PEP?
As an aside, I have tried this example in mypy (0.800), PyCharm (2020.3), pyright (in pylance 2021.6.2) and pytype (2021.05.25), and none of them seem to accept it. The same goes for typing.get_type_hints, which fails with a NameError
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
from __future__ import annotations import typing class C: ... class D: ... def method(self) -> D: ... ... typing.get_type_hints(C.D.method) Traceback (most recent call last): ... NameError: name 'D' is not defined
Cheers, Sergei _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: guido@python.org
-- --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-c...>
Thanks for confirming my suspicions, Guido. Do you think we should amend the PEP to say this should fail? On Sat, Jun 19, 2021 at 3:43 PM Guido van Rossum <guido@python.org> wrote:
You're right, this violates the standard scoping rules. I suspect it is a mistake by the PEP author. I'm adding Lukasz to the discussion, hopefully he remembers why the PEP says this.
On Sat, Jun 19, 2021 at 12:10 AM Sergei Lebedev < sergei.a.lebedev@gmail.com> wrote:
Hi typing-sig,
PEP-563 has a "Backward compatibility" <https://www.python.org/dev/peps/pep-0563/#backwards-compatibility> section which contains examples of how type annotations work for nested classes. In particular, the PEP says that
class C: class D: def method(self) -> D: ...
should be accepted by the type checker. This does not contradict
Annotations using nested classes and their respective state are still valid. They can use local names or the fully qualified name.
but is still surprising, because if we apply the usual scoping rules, the name D is not visible from the body of D. The name C is visible, so is C.D, but D is not.
Does this look right to you? If yes, could you help me understand the motivation for having this special-case in the PEP?
As an aside, I have tried this example in mypy (0.800), PyCharm (2020.3), pyright (in pylance 2021.6.2) and pytype (2021.05.25), and none of them seem to accept it. The same goes for typing.get_type_hints, which fails with a NameError
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
from __future__ import annotations import typing class C: ... class D: ... def method(self) -> D: ... ... typing.get_type_hints(C.D.method) Traceback (most recent call last): ... NameError: name 'D' is not defined
Cheers, Sergei _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: guido@python.org
-- --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-c...>
I'm not really the point of contact for this. On Tue, Jun 22, 2021 at 2:18 AM Sergei Lebedev <sergei.a.lebedev@gmail.com> wrote:
Thanks for confirming my suspicions, Guido. Do you think we should amend the PEP to say this should fail?
On Sat, Jun 19, 2021 at 3:43 PM Guido van Rossum <guido@python.org> wrote:
You're right, this violates the standard scoping rules. I suspect it is a mistake by the PEP author. I'm adding Lukasz to the discussion, hopefully he remembers why the PEP says this.
On Sat, Jun 19, 2021 at 12:10 AM Sergei Lebedev < sergei.a.lebedev@gmail.com> wrote:
Hi typing-sig,
PEP-563 has a "Backward compatibility" <https://www.python.org/dev/peps/pep-0563/#backwards-compatibility> section which contains examples of how type annotations work for nested classes. In particular, the PEP says that
class C: class D: def method(self) -> D: ...
should be accepted by the type checker. This does not contradict
Annotations using nested classes and their respective state are still valid. They can use local names or the fully qualified name.
but is still surprising, because if we apply the usual scoping rules, the name D is not visible from the body of D. The name C is visible, so is C.D, but D is not.
Does this look right to you? If yes, could you help me understand the motivation for having this special-case in the PEP?
As an aside, I have tried this example in mypy (0.800), PyCharm (2020.3), pyright (in pylance 2021.6.2) and pytype (2021.05.25), and none of them seem to accept it. The same goes for typing.get_type_hints, which fails with a NameError
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
from __future__ import annotations import typing class C: ... class D: ... def method(self) -> D: ... ... typing.get_type_hints(C.D.method) Traceback (most recent call last): ... NameError: name 'D' is not defined
Cheers, Sergei _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: guido@python.org
-- --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-c...>
-- --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-c...>
Hi Łukasz, Does this look like a mistake in the PEP to you? If yes, what is the best way forward here, in your opinion? Cheers, Sergei On Tue, Jun 22, 2021 at 7:25 PM Guido van Rossum <guido@python.org> wrote:
I'm not really the point of contact for this.
On Tue, Jun 22, 2021 at 2:18 AM Sergei Lebedev <sergei.a.lebedev@gmail.com> wrote:
Thanks for confirming my suspicions, Guido. Do you think we should amend the PEP to say this should fail?
On Sat, Jun 19, 2021 at 3:43 PM Guido van Rossum <guido@python.org> wrote:
You're right, this violates the standard scoping rules. I suspect it is a mistake by the PEP author. I'm adding Lukasz to the discussion, hopefully he remembers why the PEP says this.
On Sat, Jun 19, 2021 at 12:10 AM Sergei Lebedev < sergei.a.lebedev@gmail.com> wrote:
Hi typing-sig,
PEP-563 has a "Backward compatibility" <https://www.python.org/dev/peps/pep-0563/#backwards-compatibility> section which contains examples of how type annotations work for nested classes. In particular, the PEP says that
class C: class D: def method(self) -> D: ...
should be accepted by the type checker. This does not contradict
Annotations using nested classes and their respective state are still valid. They can use local names or the fully qualified name.
but is still surprising, because if we apply the usual scoping rules, the name D is not visible from the body of D. The name C is visible, so is C.D, but D is not.
Does this look right to you? If yes, could you help me understand the motivation for having this special-case in the PEP?
As an aside, I have tried this example in mypy (0.800), PyCharm (2020.3), pyright (in pylance 2021.6.2) and pytype (2021.05.25), and none of them seem to accept it. The same goes for typing.get_type_hints, which fails with a NameError
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
> from __future__ import annotations > import typing > class C: ... class D: ... def method(self) -> D: ... ... > typing.get_type_hints(C.D.method) Traceback (most recent call last): ... NameError: name 'D' is not defined
Cheers, Sergei _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: guido@python.org
-- --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-c...>
-- --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-c...>
participants (2)
-
Guido van Rossum
-
Sergei Lebedev