[go: up one dir, main page]

Skip to content

Commit

Permalink
fix setattr
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Yavorsky committed May 11, 2024
1 parent d27fbce commit df6ef6e
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions src/mapenv/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import logging
import os
from functools import lru_cache
from typing import Any, NewType, TypeAlias, get_args, get_origin

StrDict: TypeAlias = dict[str, str]
TypedDict = NewType("TypedDict", dict[str, Any])

log = logging.getLogger(__name__)


class MetaClass(type):
def __call__(
Expand All @@ -26,8 +23,8 @@ def __call__(
typed_dict = cls.__make_types(merged_env=merged_env)

instance = super().__call__(*args, **kwargs)
cls.__setfrozen(instance=instance, frozen=__frozen)
cls.__init_values(instance=instance, typed_dict=typed_dict)
instance.__dict__["_frozen"] = __frozen
instance.__dict__ |= typed_dict

return instance

Expand Down Expand Up @@ -97,23 +94,13 @@ def __set_type(cls, *, type_hint: type, value: str) -> Any:

return origin(val)

def __init_values(
cls, *, instance: "MetaClass", typed_dict: TypedDict
) -> None:
for k, v in typed_dict.items():
setattr(instance, k, v)

def __setfrozen(cls, *, instance: "MetaClass", frozen: bool) -> None:
name = "_frozen"
setattr(instance, name, frozen)


class MapEnv(metaclass=MetaClass):
def __str__(self) -> str:
return f"{self.__class__.__name__}{self.__dict__}"

def __setattr__(self, _name: str, _value: Any) -> None:
if getattr(self, "_frozen", False): # noqa [B009]
if _name != "__dict__" and getattr(self, "_frozen", False): # noqa:B009
if _name not in self.__annotations__:
raise TypeError(
"This object is frozen, you can't set attribute."
Expand Down

0 comments on commit df6ef6e

Please sign in to comment.