Get the namespace of the module where the object is defined. Caution: this function does not return a copy of the module namespace, so the result should not be mutated. The burden of enforcing this is on the caller.
(obj: Any)
| 46 | |
| 47 | |
| 48 | def get_module_ns_of(obj: Any) -> dict[str, Any]: |
| 49 | """Get the namespace of the module where the object is defined. |
| 50 | |
| 51 | Caution: this function does not return a copy of the module namespace, so the result |
| 52 | should not be mutated. The burden of enforcing this is on the caller. |
| 53 | """ |
| 54 | module_name = getattr(obj, '__module__', None) |
| 55 | if module_name: |
| 56 | try: |
| 57 | return sys.modules[module_name].__dict__ |
| 58 | except KeyError: |
| 59 | # happens occasionally, see https://github.com/pydantic/pydantic/issues/2363 |
| 60 | return {} |
| 61 | return {} |
| 62 | |
| 63 | |
| 64 | # Note that this class is almost identical to `collections.ChainMap`, but need to enforce |
no outgoing calls
no test coverage detected