@@ -24,6 +24,16 @@ def asdict(obj):
2424 return {f .name : getattr (obj , f .name ) for f in fields (obj )}
2525
2626
27+ def maybe_missing_field (f ) -> bool :
28+ if isinstance (f .type , str ):
29+ return f .type .startswith ("MaybeMissing[" )
30+ return (
31+ hasattr (f .type , "__origin__" )
32+ and inspect .isclass (f .type .__origin__ )
33+ and issubclass (f .type .__origin__ , MaybeMissing )
34+ )
35+
36+
2737class MaybeMissing (Generic [T ], ABC ):
2838 @abstractmethod
2939 def value (self ) -> T :
@@ -108,11 +118,7 @@ def from_json(cls, parsed: JSON) -> "Obj":
108118 kwargs = {}
109119 for f in fields (cls ):
110120 conv : Conversion = cls ._conversion [f .name ]
111- if (
112- hasattr (f .type , "__origin__" )
113- and inspect .isclass (f .type .__origin__ )
114- and issubclass (f .type .__origin__ , MaybeMissing )
115- ):
121+ if maybe_missing_field (f ):
116122 kwargs [f .name ] = (
117123 Jst (conv .from_json (parsed [f .name ])) if f .name in parsed else Miss ()
118124 )
@@ -174,11 +180,7 @@ def from_json(cls, parsed: JSON) -> "Obj":
174180 for field in fields (cls ):
175181 conv : Conversion = cls ._conversion [field .name ]
176182 maybe_unescaped_name = cls ._maybe_renamed (field .name )
177- if (
178- hasattr (field .type , "__origin__" )
179- and inspect .isclass (field .type .__origin__ )
180- and issubclass (field .type .__origin__ , MaybeMissing )
181- ):
183+ if maybe_missing_field (field ):
182184 kwargs [field .name ] = (
183185 Jst (conv .from_json (parsed [maybe_unescaped_name ]))
184186 if maybe_unescaped_name in parsed
0 commit comments