99 # Types that can be converted into InternationalString
1010 Convertible = str | Sequence | Mapping | Iterable [tuple [str , str ]]
1111
12- TInternationalString : TypeAlias = "InternationalString" | "Convertible"
13- TInternationalStringInit = TInternationalString | None
12+ TInternationalString : TypeAlias = "InternationalString" | Convertible
1413
1514# TODO read this from the environment, or use any value set in the SDMX-ML spec.
1615# Currently set to 'en' because test_dsd.py expects it.
2322class InternationalString :
2423 """SDMX-IM InternationalString.
2524
26- SDMX-IM LocalisedString is not implemented. Instead, the ' localizations' is a
27- mapping where :
25+ SDMX-IM LocalisedString is not implemented. Instead, :attr:` localizations` is a
26+ mapping in which :
2827
2928 - keys correspond to the 'locale' property of LocalisedString.
3029 - values correspond to the 'label' property of LocalisedString.
3130
32- When used as a type hint with pydantic, InternationalString fields can be assigned
33- to in one of four ways::
31+ When :class:`InternationalStringDescriptor` is used as a dataclass field type, the
32+ field can be assigned in one of four ways::
3433
3534 @dataclass
3635 class Foo:
@@ -55,7 +54,6 @@ class Foo:
5554
5655 Only the first method preserves existing localizations; the latter three replace
5756 them.
58-
5957 """
6058
6159 __slots__ = ("localizations" ,)
@@ -64,30 +62,24 @@ class Foo:
6462
6563 def __init__ (self , value : "Convertible | None" = None , ** kwargs ) -> None :
6664 # Handle initial values according to type
67- if value is None :
68- # Keyword arguments → dict, possibly empty
69- value = dict (kwargs )
70- elif isinstance (value , str ):
71- # Bare string
72- value = {DEFAULT_LOCALE : value }
73- elif (
74- isinstance (value , Sequence )
75- and len (value ) == 2
76- and isinstance (value [0 ], str )
77- ):
78- # 2-tuple of str is (locale, label)
79- value = {value [0 ]: value [1 ]}
80- elif isinstance (value , Mapping ):
81- # dict; use directly
82- value = dict (value )
83- else :
84- try :
85- # Iterable of 2-tuples
86- value = { # type: ignore [misc]
87- locale : label for (locale , label ) in value
88- }
89- except Exception :
90- raise ValueError (value , kwargs )
65+ match value :
66+ case None : # Keyword arguments → dict, possibly empty
67+ value = dict (kwargs )
68+ case str (): # Bare string
69+ value = {DEFAULT_LOCALE : value }
70+ case Sequence () if len (value ) == 2 and isinstance (value [0 ], str ):
71+ # 2-tuple of str is (locale, label)
72+ value = {value [0 ]: value [1 ]}
73+ case Mapping (): # dict or similar; use directly
74+ value = dict (value )
75+ case _:
76+ try :
77+ # Iterable of 2-tuples
78+ value = { # type: ignore [misc]
79+ locale : label for (locale , label ) in value
80+ }
81+ except Exception :
82+ raise ValueError (value , kwargs )
9183
9284 self .localizations = value
9385
0 commit comments