[issue46539] typing: forward references don't understand special type forms

2022-01-27 Thread Alex Waygood


Change by Alex Waygood :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-27 Thread miss-islington


miss-islington  added the comment:


New changeset bfcb41420a326ec353740d8180ffbf402746fa33 by Miss Islington (bot) 
in branch '3.10':
bpo-46539: Pass status of special typeforms to forward references (GH-30926)
https://github.com/python/cpython/commit/bfcb41420a326ec353740d8180ffbf402746fa33


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-27 Thread miss-islington


miss-islington  added the comment:


New changeset 37577033baadf5f4a30d0998bae7d26f11a694e2 by Miss Islington (bot) 
in branch '3.9':
bpo-46539: Pass status of special typeforms to forward references (GH-30926)
https://github.com/python/cpython/commit/37577033baadf5f4a30d0998bae7d26f11a694e2


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread miss-islington


Change by miss-islington :


--
pull_requests: +29126
pull_request: https://github.com/python/cpython/pull/30947

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread miss-islington


Change by miss-islington :


--
nosy: +miss-islington
nosy_count: 6.0 -> 7.0
pull_requests: +29125
pull_request: https://github.com/python/cpython/pull/30946

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread Guido van Rossum


Guido van Rossum  added the comment:


New changeset ced50051bb752a7c1e616f4b0c001f37f0354f32 by Gregory Beauregard in 
branch 'main':
bpo-46539: Pass status of special typeforms to forward references (GH-30926)
https://github.com/python/cpython/commit/ced50051bb752a7c1e616f4b0c001f37f0354f32


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread Guido van Rossum


Guido van Rossum  added the comment:

Agree it's not the same issue, but there's similarity -- both are due to 
putting a stringized annotation (presumably a forward ref) somewhere inside 
another construct. whether it's list["N"] or Annotated["ClassVar[int]"].

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread Gregory Beauregard


Gregory Beauregard  added the comment:

I did try the proposed patch in bpo-41370 and verified it didn't resolve the 
issue so I'm not certain they strictly overlap, but I also haven't had time to 
fully digest the underlying issues in bpo-41370 yet.

I think it does have relevance for changes we want to make for dataclasses re: 
Annotated, though: https://bugs.python.org/issue46511

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread Guido van Rossum


Guido van Rossum  added the comment:

I wonder if this is at all similar to bpo-41370.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread Gregory Beauregard


Gregory Beauregard  added the comment:

typo: the line is `g: Annotated["ClassVar[int]", (2, 5)] = 3` in the code 
sample. Somehow I left it at only `(` for the annotation instead of `(2,5)`

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread Gregory Beauregard


Change by Gregory Beauregard :


--
type:  -> behavior

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread Alex Waygood


Change by Alex Waygood :


--
nosy: +AlexWaygood, sobolevn

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread Gregory Beauregard


Change by Gregory Beauregard :


--
keywords: +patch
pull_requests: +29105
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/30926

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46539] typing: forward references don't understand special type forms

2022-01-26 Thread Gregory Beauregard


New submission from Gregory Beauregard :

Consider the following code on 3.11 main:

```
from typing import Annotated, ClassVar, get_type_hints

class DC:
a: ClassVar[int] = 3
b: ClassVar["int"] = 3
c: "ClassVar[int]" = 3
d: Annotated[ClassVar[int], (2, 5)] = 3
e: Annotated[ClassVar["int"], (2, 5)] = 3
f: "Annotated[ClassVar[int], (2, 5)]" = 3

class DC_Special_ForwardRef:
g: Annotated["ClassVar[int]", (] = 3

# OK
assert get_type_hints(DC, globals(), locals()) == {
"a": ClassVar[int],
"b": ClassVar[int],
"c": ClassVar[int],
"d": ClassVar[int],
"e": ClassVar[int],
"f": ClassVar[int],
}

# TypeError: typing.ClassVar[int] is not valid as type argument
get_type_hints(DC_Special_ForwardRef, globals(), locals())
```

Currently, the `Annotated["ClassVar[int]", (2, 5)]` annotation raises at 
runtime when `get_type_hints` is called, but all the other forward reference 
annotations are okay.

My understanding is this is because when typing._type_check runs on a type 
where special forms are allowed it's possible for the typing._type_convert it 
calls it itself run a typing._type_check on contained forward references. 
However, if that forward reference was itself a special form then it's possible 
to get an error because typing._type_check doesn't pass on that special forms 
are allowed.

I have drafted a patch to pass on this information. This will become important 
in the future as more special forms are allowed to wrap each other, such as 
allowing Final and ClassVar to nest each other in dataclasses, or when Required 
and NotRequired land. In the future we may also want to reconsider runtime 
restrictions on special forms in `typing.py` entirely and instead choose to 
leave this to type checkers.

Is my analysis/patch approach okay? Forward references can be tricky. Should we 
be discussing runtime restrictions in typing.py more generally in the future?

--
components: Library (Lib)
messages: 411790
nosy: GBeauregard, Jelle Zijlstra, gvanrossum, kj
priority: normal
severity: normal
status: open
title: typing: forward references don't understand special type forms
versions: Python 3.10, Python 3.11, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com