Skip to content

Commit 980c956

Browse files
committed
Make SettingsDict a UserDict
This allows us to set arbitrary attributes and then later retrieve them from the frame locals.
1 parent 2b78d85 commit 980c956

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

class_settings/env.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ def __init__(self):
2323
self.parser(parser)
2424

2525
def __call__(self, name=None, *, prefix=missing, default=missing, optional=False):
26+
from .settings import SettingsDict
27+
2628
frame = sys._getframe(1)
2729
while frame is not None:
28-
options = frame.f_locals.get("__options__")
29-
if isinstance(options, Options):
30+
f_locals = frame.f_locals
31+
if isinstance(f_locals, SettingsDict):
32+
options = f_locals.options
3033
break
3134
frame = frame.f_back
3235
else:

class_settings/settings.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import __future__
22

33
import ast
4+
import collections
45
import copy
56
import inspect
67
import sys
@@ -13,9 +14,10 @@
1314
from .options import Options
1415

1516

16-
class SettingsDict(dict):
17+
class SettingsDict(collections.UserDict):
1718
def __init__(self, *, options, bases):
18-
super().__init__(__options__=options)
19+
super().__init__()
20+
self.options = options
1921
if options.inject_settings:
2022
to_inject = {}
2123
for base in reversed(bases):
@@ -75,13 +77,10 @@ def __prepare__(name, bases):
7577
return SettingsDict(options=Options(meta), bases=bases)
7678

7779
def __new__(meta, name, bases, namespace):
78-
options = namespace.pop("__options__", None)
79-
if not isinstance(options, Options):
80-
raise AttributeError("__options__ cannot be overwritten")
8180
if "Meta" in namespace and not isinstance(namespace["Meta"], type):
8281
raise TypeError("{}.Meta has to be a class".format(name))
83-
namespace["_options"] = options
84-
return super().__new__(meta, name, bases, namespace)
82+
namespace["_options"] = namespace.options
83+
return super().__new__(meta, name, bases, namespace.data)
8584

8685
def __dir__(cls):
8786
default_settings = cls._options.default_settings

0 commit comments

Comments
 (0)