1010from __future__ import annotations
1111
1212from collections .abc import Mapping , Sequence
13- from typing import TYPE_CHECKING , Literal , Optional , Union
13+ from typing import TYPE_CHECKING , Literal , NotRequired , Optional , Union
1414
1515import pytest
1616
1717# ``parse_json`` (the module under test) uses ``typing_extensions`` for its
1818# TypedDict bookkeeping, so we build TypedDicts the same way for consistency.
19- from typing_extensions import NotRequired , TypedDict
19+ from typing_extensions import TypedDict
2020
2121from zarr .core .json_parse import parse_json
2222
@@ -159,7 +159,7 @@ def test_literal_one_does_not_match_bool_literal() -> None:
159159
160160
161161def test_literal_mixed_members () -> None :
162- annotation = Literal ["a" , 3 , None ]
162+ annotation = Literal ["a" , 3 ] | None
163163 assert parse_json ("a" , annotation ) == "a"
164164 assert parse_json (3 , annotation ) == 3
165165 # ``None`` is a valid literal member.
@@ -169,11 +169,15 @@ def test_literal_mixed_members() -> None:
169169# ---------------------------------------------------------------------------
170170# Union / Optional
171171# ---------------------------------------------------------------------------
172+ # These tests deliberately use the ``typing.Union`` / ``typing.Optional``
173+ # spelling (which yields a ``typing.Union`` origin) to cover that code path;
174+ # the ``X | Y`` spelling (``types.UnionType``) is covered separately below.
175+ # The legacy-spelling lines therefore suppress ruff's UP007/UP045 rules.
172176
173177
174178@pytest .mark .parametrize ("value" , ["hello" , 5 ])
175179def test_union_each_member_accepted (value : Any ) -> None :
176- assert parse_json (value , Union [str , int ]) == value
180+ assert parse_json (value , Union [str , int ]) == value # noqa: UP007
177181
178182
179183def test_union_pep604_syntax () -> None :
@@ -184,27 +188,27 @@ def test_union_pep604_syntax() -> None:
184188
185189def test_union_no_member_matches_raises () -> None :
186190 with pytest .raises (ValueError , match = "Expected a value matching one of" ):
187- parse_json (1.5 , Union [str , int ])
191+ parse_json (1.5 , Union [str , int ]) # noqa: UP007
188192
189193
190194def test_union_error_lists_each_member () -> None :
191195 with pytest .raises (ValueError , match = "Tried each union member" ):
192- parse_json ([], Union [str , int ])
196+ parse_json ([], Union [str , int ]) # noqa: UP007
193197
194198
195199@pytest .mark .parametrize ("value" , [None , "x" ])
196200def test_optional_accepts_none_and_value (value : Any ) -> None :
197- assert parse_json (value , Optional [str ]) == value
201+ assert parse_json (value , Optional [str ]) == value # noqa: UP045
198202
199203
200204def test_optional_rejects_other_types () -> None :
201205 with pytest .raises (ValueError , match = "Expected a value matching one of" ):
202- parse_json (5 , Optional [str ])
206+ parse_json (5 , Optional [str ]) # noqa: UP045
203207
204208
205209def test_union_with_coercion_returns_coerced_value () -> None :
206210 """A union member that coerces (Sequence -> tuple) returns the coerced form."""
207- result = parse_json ([1 , 2 , 3 ], Union [str , Sequence [int ]])
211+ result = parse_json ([1 , 2 , 3 ], Union [str , Sequence [int ]]) # noqa: UP007
208212 assert result == (1 , 2 , 3 )
209213 assert isinstance (result , tuple )
210214
@@ -358,15 +362,14 @@ class Point(TypedDict):
358362
359363
360364# NOTE: This module uses ``from __future__ import annotations``, which stringizes
361- # annotations. With the *class* TypedDict syntax, ``typing_extensions`` cannot see
362- # a ``NotRequired[...]`` wrapper at class-creation time (the annotation is a string),
363- # so the key is wrongly recorded as required. This is a known limitation of
364- # stringized annotations + ``NotRequired`` -- NOT a bug in ``json_parse``. The
365- # functional ``TypedDict(...)`` form keeps ``NotRequired`` as a real runtime object,
366- # so ``__optional_keys__`` is populated correctly; we use it here to genuinely
367- # exercise NotRequired. (``total=False``, used by ``PartialConfig`` below, is
368- # class-level and works correctly even with stringized annotations.)
369- Config = TypedDict ("Config" , {"name" : str , "count" : NotRequired [int ]})
365+ # annotations -- so a class-syntax ``NotRequired[...]`` is invisible to the
366+ # TypedDict's ``__optional_keys__``. ``_parse_typeddict`` handles this by
367+ # resolving the hints with ``get_type_hints(..., include_extras=True)`` and
368+ # reading the ``Required`` / ``NotRequired`` wrappers directly, so the class
369+ # form below genuinely exercises NotRequired under stringized annotations.
370+ class Config (TypedDict ):
371+ name : str
372+ count : NotRequired [int ]
370373
371374
372375class PartialConfig (TypedDict , total = False ):
0 commit comments