MCPcopy
hub / github.com/fluentpython/example-code-2e / checked

Function checked

24-class-metaprog/checked/decorator/checkeddeco.py:96–112  ·  view source on GitHub ↗
(cls: type)

Source from the content-addressed store, hash-verified

94
95# tag::CHECKED_DECORATOR[]
96def checked(cls: type) -> type: # <1>
97 for name, constructor in _fields(cls).items(): # <2>
98 setattr(cls, name, Field(name, constructor)) # <3>
99
100 cls._fields = classmethod(_fields) # type: ignore # <4>
101
102 instance_methods = ( # <5>
103 __init__,
104 __repr__,
105 __setattr__,
106 _asdict,
107 __flag_unknown_attrs,
108 )
109 for method in instance_methods: # <6>
110 setattr(cls, method.__name__, method)
111
112 return cls # <7>
113# end::CHECKED_DECORATOR[]
114
115# tag::CHECKED_METHODS[]

Callers

nothing calls this directly

Calls 3

_fieldsFunction · 0.85
FieldClass · 0.70
itemsMethod · 0.45

Tested by

no test coverage detected