Skip to content
Merged
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
10 changes: 6 additions & 4 deletions cccv/util/registry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved

# pyre-strict
# pyre-ignore-all-errors[2,3]
import warnings
from typing import Any, Dict, Iterable, Iterator, Optional, Tuple


Expand Down Expand Up @@ -41,7 +39,11 @@ def __init__(self, name: str) -> None:

def _do_register(self, name: str, obj: Any) -> None:
if name in self._obj_map:
raise KeyError(f"[CCCV] An object named '{name}' was already registered in '{self._name}' registry!")
warnings.warn(
f"[CCCV] An object named '{name}' was already registered in '{self._name}' registry! We will NOT overwrite the existing one.",
UserWarning,
stacklevel=2,
)
Comment on lines +42 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current stacklevel=2 will cause the warning to point to the register method within this file, which is not very helpful for the end-user. To make the warning more useful, it should point to the line in the user's code where the duplicate registration is attempted. Changing stacklevel to 3 will achieve this by moving up the call stack to the user's code, making it much easier to locate the source of the duplicate registration.

Suggested change
warnings.warn(
f"[CCCV] An object named '{name}' was already registered in '{self._name}' registry! We will NOT overwrite the existing one.",
UserWarning,
stacklevel=2,
)
warnings.warn(
f"[CCCV] An object named '{name}' was already registered in '{self._name}' registry! We will NOT overwrite the existing one.",
UserWarning,
stacklevel=3,
)

else:
self._obj_map[name] = obj

Expand Down
Loading