Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow alias list to contain default/derived/provided name. #43

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions autoregistry/_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,38 @@ def register(
elif "." in name or "/" in name:
raise InvalidNameError(f'Name "{name}" cannot contain "." or "/".')

if not self.config.overwrite and name in self:
raise KeyCollisionError(f'"{name}" already registered to {self}')

# Validate aliases and massage it into a list.
if aliases is None:
aliases = []
elif isinstance(aliases, str):
aliases = [aliases]
else:
aliases = list(aliases)

# Only use ``aliases_set`` to detect "in", don't iterate since order is not preserved.
aliases_set = set(aliases)
if len(aliases_set) != len(aliases):
raise KeyCollisionError("Cannot provide multiple aliases with same name.")

if name not in aliases_set:
# It may be useful to redundantly define ``name`` in ``aliases``.
aliases.insert(0, name)

# Validate aliases
for alias in aliases:
if "." in alias or "/" in alias:
raise InvalidNameError(f'Alias "{alias}" cannot contain "." or "/".')
if not self.config.overwrite and alias in self:
raise KeyCollisionError(f'"{alias}" already registered to {self}')

# Check if should register self
if obj == self.cls:
if self.config.register_self:
self[name] = obj
else:
self[name] = obj
# Register name and aliases
for alias in aliases:
# Check if should register self
if obj == self.cls:
if self.config.register_self:
self[alias] = obj
else:
self[alias] = obj

# Register to parents if one of the following conditions are met:
# 1. This is the root ``__recursive__`` call.
Expand All @@ -130,13 +141,6 @@ def register(
if root or parent_registry.config.recursive:
parent_registry.register(obj, name=name, aliases=aliases)

# Register aliases
for alias in aliases:
if not self.config.overwrite and alias in self:
raise KeyCollisionError(f'"{alias}" already registered to {self}')

self[alias] = obj


class _DictMixin:
"""Dict-like methods for a registry-based class."""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_classes_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,19 @@ class Temperature(Sensor):
assert Sensor["oxygen"] == Sensor["o2"] == Sensor["air"] == Oxygen


def test_aliases_list_self_collision():
"""Having an alias be the same as ``name`` should NOT generate a KeyCollisionError."""

class Sensor(Registry):
pass

class Oxygen(Sensor, aliases=["oxygen"]):
pass

assert list(Sensor.keys()) == ["oxygen"]
assert Sensor["oxygen"] == Oxygen


def test_aliases_single_str_invalid():
class Sensor(Registry):
pass
Expand Down
Loading