@@ -569,22 +569,30 @@ def target_lang(self) -> str:
569569 return self ._target_lang
570570
571571
572+ def _required (json , key : str ):
573+ """Returns json[key]; raises KeyError if the field is missing or null."""
574+ value = json [key ]
575+ if value is None :
576+ raise KeyError (key )
577+ return value
578+
579+
572580class ResourceFeature :
573581 """A feature supported by a DeepL API resource, as returned by
574582 DeepLClient.get_language_resources().
575583
576584 :param name: The feature name, for example "glossary" or "formality".
577585 :param needs_source_support: True if the source language must support
578- the feature for it to be available, None if not specified .
586+ the feature for it to be available; False if the API omits it .
579587 :param needs_target_support: True if the target language must support
580- the feature for it to be available, None if not specified .
588+ the feature for it to be available; False if the API omits it .
581589 """
582590
583591 def __init__ (
584592 self ,
585593 name : str ,
586- needs_source_support : Optional [ bool ] = None ,
587- needs_target_support : Optional [ bool ] = None ,
594+ needs_source_support : bool = False ,
595+ needs_target_support : bool = False ,
588596 ):
589597 self ._name = name
590598 self ._needs_source_support = needs_source_support
@@ -594,9 +602,9 @@ def __init__(
594602 def from_json (json ) -> "ResourceFeature" :
595603 """Create ResourceFeature from the given API JSON object."""
596604 return ResourceFeature (
597- name = json [ "name" ] ,
598- needs_source_support = json .get ("needs_source_support" ),
599- needs_target_support = json .get ("needs_target_support" ),
605+ name = _required ( json , "name" ) ,
606+ needs_source_support = bool ( json .get ("needs_source_support" ) ),
607+ needs_target_support = bool ( json .get ("needs_target_support" ) ),
600608 )
601609
602610 def __str__ (self ) -> str :
@@ -608,15 +616,15 @@ def name(self) -> str:
608616 return self ._name
609617
610618 @property
611- def needs_source_support (self ) -> Optional [ bool ] :
619+ def needs_source_support (self ) -> bool :
612620 """Returns True if the source language must support this feature,
613- or None if the API did not specify it."""
621+ False otherwise (also when the API did not specify it) ."""
614622 return self ._needs_source_support
615623
616624 @property
617- def needs_target_support (self ) -> Optional [ bool ] :
625+ def needs_target_support (self ) -> bool :
618626 """Returns True if the target language must support this feature,
619- or None if the API did not specify it."""
627+ False otherwise (also when the API did not specify it) ."""
620628 return self ._needs_target_support
621629
622630
@@ -641,7 +649,7 @@ def from_json(json) -> "LanguageResource":
641649 for feature in json .get ("features" ) or []
642650 ]
643651 return LanguageResource (
644- name = json [ "name" ] ,
652+ name = _required ( json , "name" ) ,
645653 features = {feature .name : feature for feature in features },
646654 )
647655
@@ -2271,7 +2279,7 @@ def __init__(self, status: str):
22712279 @staticmethod
22722280 def from_json (json ) -> "LanguageFeature" :
22732281 """Create LanguageFeature from the given API JSON object."""
2274- return LanguageFeature (status = json [ "status" ] )
2282+ return LanguageFeature (status = _required ( json , "status" ) )
22752283
22762284 @property
22772285 def status (self ) -> str :
@@ -2319,11 +2327,11 @@ def from_json(json) -> "LanguageSupport":
23192327 """Create LanguageSupport from the given API JSON object."""
23202328 features = json .get ("features" ) or {}
23212329 return LanguageSupport (
2322- code = json [ "lang" ] ,
2323- name = json [ "name" ] ,
2324- status = json [ "status" ] ,
2325- usable_as_source = bool (json [ "usable_as_source" ] ),
2326- usable_as_target = bool (json [ "usable_as_target" ] ),
2330+ code = _required ( json , "lang" ) ,
2331+ name = _required ( json , "name" ) ,
2332+ status = _required ( json , "status" ) ,
2333+ usable_as_source = bool (_required ( json , "usable_as_source" ) ),
2334+ usable_as_target = bool (_required ( json , "usable_as_target" ) ),
23272335 features = {
23282336 name : LanguageFeature .from_json (feature )
23292337 for name , feature in features .items ()
0 commit comments