Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
natelust committed Feb 8, 2023
1 parent 6738b58 commit 7772f94
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 115 deletions.
2 changes: 1 addition & 1 deletion python/lsst/pex/config/configurableActions/__init__.py
Expand Up @@ -19,5 +19,5 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from ._configurableAction import *
from ._configurableActionField import *
from ._configurableActionStructField import *
from ._configurableActionField import *
Expand Up @@ -26,8 +26,7 @@

from lsst.pex.config.config import Config


ActionTypeVar = TypeVar("ActionTypeVar", bound='ConfigurableAction')
ActionTypeVar = TypeVar("ActionTypeVar", bound="ConfigurableAction")


class ConfigurableAction(Config):
Expand Down Expand Up @@ -56,7 +55,7 @@ class ConfigurableAction(Config):
"""

def __setattr__(self, attr, value, at=None, label="assignment"):
if attr == 'identity':
if attr == "identity":
return object.__setattr__(self, attr, value)
return super().__setattr__(attr, value, at, label)

Expand Down
Expand Up @@ -24,11 +24,11 @@

from typing import Any, overload

from lsst.pex.config import ConfigField, FieldValidationError, Config
from lsst.pex.config.config import _typeStr, _joinNamePath
from lsst.pex.config import Config, ConfigField, FieldValidationError
from lsst.pex.config.callStack import getCallStack
from lsst.pex.config.config import _joinNamePath, _typeStr

from . import ConfigurableAction, ActionTypeVar
from . import ActionTypeVar, ConfigurableAction


class ConfigurableActionField(ConfigField[ActionTypeVar]):
Expand All @@ -40,6 +40,7 @@ class ConfigurableActionField(ConfigField[ActionTypeVar]):
Any configuration that is done prior to reasignment to a new
`ConfigurableAction` is forgotten.
"""

# These attributes are dynamically assigned when constructing the base
# classes
name: str
Expand All @@ -49,11 +50,10 @@ def __set__(
instance: Config,
value: ActionTypeVar | type[ActionTypeVar],
at: Any = None,
label: str = "assignment"
label: str = "assignment",
) -> None:
if instance._frozen:
raise FieldValidationError(self, instance,
"Cannot modify a frozen Config")
raise FieldValidationError(self, instance, "Cannot modify a frozen Config")
name = _joinNamePath(prefix=instance._name, name=self.name)

if not isinstance(value, self.dtype) and not issubclass(value, self.dtype):
Expand All @@ -64,8 +64,7 @@ def __set__(
at = getCallStack()

if isinstance(value, self.dtype):
instance._storage[self.name] = type(value)(__name=name, __at=at,
__label=label, **value._storage)
instance._storage[self.name] = type(value)(__name=name, __at=at, __label=label, **value._storage)
else:
instance._storage[self.name] = value(__name=name, __at=at, __label=label)
history = instance._history.setdefault(self.name, [])
Expand All @@ -78,9 +77,7 @@ def __get__(
...

@overload
def __get__(
self, instance: "Config", owner: Any = None, at: Any = None, label: str = "default"
) -> Any:
def __get__(self, instance: "Config", owner: Any = None, at: Any = None, label: str = "default") -> Any:
...

def __get__(self, instance, owner=None, at=None, label="default"):
Expand Down
Expand Up @@ -22,31 +22,29 @@

__all__ = ("ConfigurableActionStructField", "ConfigurableActionStruct")

from types import SimpleNamespace
import weakref
from types import GenericAlias, SimpleNamespace
from typing import (
Any,
Dict,
Generic,
Iterable,
Iterator,
List,
Mapping,
Optional,
Tuple,
Type,
TypeVar,
Union,
Type,
Tuple,
List,
Any,
Dict,
Iterator,
Generic,
overload
overload,
)
from types import GenericAlias

from lsst.pex.config.config import Config, Field, FieldValidationError, _typeStr, _joinNamePath
from lsst.pex.config.comparison import compareConfigs, compareScalars, getComparisonName
from lsst.pex.config.callStack import StackFrame, getCallStack, getStackFrame
from lsst.pex.config.comparison import compareConfigs, compareScalars, getComparisonName
from lsst.pex.config.config import Config, Field, FieldValidationError, _joinNamePath, _typeStr

from . import ConfigurableAction, ActionTypeVar

import weakref
from . import ActionTypeVar, ConfigurableAction


class ConfigurableActionStructUpdater:
Expand All @@ -55,17 +53,22 @@ class ConfigurableActionStructUpdater:
useful in the context of setting configuration through pipelines or on
the command line.
"""
def __set__(self, instance: ConfigurableActionStruct,
value: Union[Mapping[str, ConfigurableAction], ConfigurableActionStruct]) -> None:

def __set__(
self,
instance: ConfigurableActionStruct,
value: Union[Mapping[str, ConfigurableAction], ConfigurableActionStruct],
) -> None:
if isinstance(value, Mapping):
pass
elif isinstance(value, ConfigurableActionStruct):
# If the update target is a ConfigurableActionStruct, get the
# internal dictionary
value = value._attrs
else:
raise ValueError("Can only update a ConfigurableActionStruct with an instance of such, or a "
"mapping")
raise ValueError(
"Can only update a ConfigurableActionStruct with an instance of such, or a " "mapping"
)
for name, action in value.items():
setattr(instance, name, action)

Expand All @@ -86,13 +89,13 @@ class ConfigurableActionStructRemover:
Raised if an attribute specified for removal does not exist in the
ConfigurableActionStruct
"""
def __set__(self, instance: ConfigurableActionStruct,
value: Union[str, Iterable[str]]) -> None:

def __set__(self, instance: ConfigurableActionStruct, value: Union[str, Iterable[str]]) -> None:
# strings are iterable, but not in the way that is intended. If a
# single name is specified, turn it into a tuple before attempting
# to remove the attribute
if isinstance(value, str):
value = (value, )
value = (value,)
for name in value:
delattr(instance, name)

Expand Down Expand Up @@ -132,6 +135,7 @@ class ConfigurableActionStruct(Generic[ActionTypeVar]):
raised. Any attributes in the Iterable prior to the name which raises will
have been removed from the `ConfigurableActionStruct`
"""

# declare attributes that are set with __setattr__
_config_: weakref.ref
_attrs: Dict[str, ActionTypeVar]
Expand All @@ -142,12 +146,18 @@ class ConfigurableActionStruct(Generic[ActionTypeVar]):
update = ConfigurableActionStructUpdater()
remove = ConfigurableActionStructRemover()

def __init__(self, config: Config, field: ConfigurableActionStructField,
value: Mapping[str, ConfigurableAction], at: Any, label: str):
object.__setattr__(self, '_config_', weakref.ref(config))
object.__setattr__(self, '_attrs', {})
object.__setattr__(self, '_field', field)
object.__setattr__(self, '_history', [])
def __init__(
self,
config: Config,
field: ConfigurableActionStructField,
value: Mapping[str, ConfigurableAction],
at: Any,
label: str,
):
object.__setattr__(self, "_config_", weakref.ref(config))
object.__setattr__(self, "_attrs", {})
object.__setattr__(self, "_field", field)
object.__setattr__(self, "_history", [])

self.history.append(("Struct initialized", at, label))

Expand All @@ -160,7 +170,7 @@ def _config(self) -> Config:
# Config Fields should never outlive their config class instance
# assert that as such here
value = self._config_()
assert(value is not None)
assert value is not None
return value

@property
Expand All @@ -171,12 +181,16 @@ def history(self) -> List[tuple]:
def fieldNames(self) -> Iterable[str]:
return self._attrs.keys()

def __setattr__(self, attr: str, value: Union[ActionTypeVar, Type[ActionTypeVar]],
at=None, label='setattr', setHistory=False) -> None:

if hasattr(self._config, '_frozen') and self._config._frozen:
msg = "Cannot modify a frozen Config. "\
f"Attempting to set item {attr} to value {value}"
def __setattr__(
self,
attr: str,
value: Union[ActionTypeVar, Type[ActionTypeVar]],
at=None,
label="setattr",
setHistory=False,
) -> None:
if hasattr(self._config, "_frozen") and self._config._frozen:
msg = "Cannot modify a frozen Config. " f"Attempting to set item {attr} to value {value}"
raise FieldValidationError(self._field, self._config, msg)

# verify that someone has not passed a string with a space or leading
Expand All @@ -198,7 +212,7 @@ def __setattr__(self, attr: str, value: Union[ActionTypeVar, Type[ActionTypeVar]
super().__setattr__(attr, value)

def __getattr__(self, attr) -> Any:
if attr in object.__getattribute__(self, '_attrs'):
if attr in object.__getattribute__(self, "_attrs"):
result = self._attrs[attr]
result.identity = attr
return result
Expand Down Expand Up @@ -245,26 +259,43 @@ class ConfigurableActionStructField(Field[ActionTypeVar]):
name: str
default: Optional[Mapping[str, ConfigurableAction]]

def __init__(self, doc: str, default: Optional[Mapping[str, ConfigurableAction]] = None,
optional: bool = False,
deprecated=None):
def __init__(
self,
doc: str,
default: Optional[Mapping[str, ConfigurableAction]] = None,
optional: bool = False,
deprecated=None,
):
source = getStackFrame()
self._setup(doc=doc, dtype=self.__class__, default=default, check=None,
optional=optional, source=source, deprecated=deprecated)
self._setup(
doc=doc,
dtype=self.__class__,
default=default,
check=None,
optional=optional,
source=source,
deprecated=deprecated,
)

def __class_getitem__(cls, params):
return GenericAlias(cls, params)

def __set__(self, instance: Config,
value: Union[None, Mapping[str, ConfigurableAction],
SimpleNamespace,
ConfigurableActionStruct,
ConfigurableActionStructField,
Type[ConfigurableActionStructField]],
at: Iterable[StackFrame] = None, label: str = 'assigment'):
def __set__(
self,
instance: Config,
value: Union[
None,
Mapping[str, ConfigurableAction],
SimpleNamespace,
ConfigurableActionStruct,
ConfigurableActionStructField,
Type[ConfigurableActionStructField],
],
at: Iterable[StackFrame] = None,
label: str = "assigment",
):
if instance._frozen:
msg = "Cannot modify a frozen Config. "\
"Attempting to set field to value %s" % value
msg = "Cannot modify a frozen Config. " "Attempting to set field to value %s" % value
raise FieldValidationError(self, instance, msg)

if at is None:
Expand All @@ -284,46 +315,35 @@ def __set__(self, instance: Config,
value = self.StructClass(instance, self, vars(value), at=at, label=label)

elif type(value) == ConfigurableActionStructField:
raise ValueError("ConfigurableActionStructFields can only be used in a class body declaration"
f"Use a {self.StructClass}, SimpleNamespace or Struct")
raise ValueError(
"ConfigurableActionStructFields can only be used in a class body declaration"
f"Use a {self.StructClass}, SimpleNamespace or Struct"
)
else:
raise ValueError(f"Unrecognized value {value}, cannot be assigned to this field")

history = instance._history.setdefault(self.name, [])
history.append((value, at, label))

if not isinstance(value, ConfigurableActionStruct):
raise FieldValidationError(self, instance,
"Can only assign things that are subclasses of Configurable Action")
raise FieldValidationError(
self, instance, "Can only assign things that are subclasses of Configurable Action"
)
instance._storage[self.name] = value

@overload
def __get__(
self,
instance: None,
owner: Any = None,
at: Any = None,
label: str = 'default'
self, instance: None, owner: Any = None, at: Any = None, label: str = "default"
) -> ConfigurableActionStruct[ActionTypeVar]:
...

@overload
def __get__(
self,
instance: Config,
owner: Any = None,
at: Any = None,
label: str = 'default'
self, instance: Config, owner: Any = None, at: Any = None, label: str = "default"
) -> ConfigurableActionStruct[ActionTypeVar]:
...

def __get__(
self,
instance,
owner=None,
at=None,
label='default'
):
def __get__(self, instance, owner=None, at=None, label="default"):
if instance is None or not isinstance(instance, Config):
return self
else:
Expand Down Expand Up @@ -364,7 +384,7 @@ def save(self, outfile, instance):
return

for _, v in sorted(actionStruct.items()):
outfile.write(u"{}={}()\n".format(v._name, _typeStr(v)))
outfile.write("{}={}()\n".format(v._name, _typeStr(v)))
v._save(outfile)

def freeze(self, instance):
Expand Down Expand Up @@ -411,16 +431,16 @@ def _compare(self, instance1, instance2, shortcut, rtol, atol, output):
d1: ConfigurableActionStruct = getattr(instance1, self.name)
d2: ConfigurableActionStruct = getattr(instance2, self.name)
name = getComparisonName(
_joinNamePath(instance1._name, self.name),
_joinNamePath(instance2._name, self.name)
_joinNamePath(instance1._name, self.name), _joinNamePath(instance2._name, self.name)
)
if not compareScalars(f"keys for {name}", set(d1.fieldNames), set(d2.fieldNames), output=output):
return False
equal = True
for k, v1 in d1.items():
v2 = getattr(d2, k)
result = compareConfigs(f"{name}.{k}", v1, v2, shortcut=shortcut,
rtol=rtol, atol=atol, output=output)
result = compareConfigs(
f"{name}.{k}", v1, v2, shortcut=shortcut, rtol=rtol, atol=atol, output=output
)
if not result and shortcut:
return False
equal = equal and result
Expand Down

0 comments on commit 7772f94

Please sign in to comment.