Skip to content

Commit

Permalink
Fix type issue found by mypy 1.11
Browse files Browse the repository at this point in the history
We use a default_key=None in the log handling code to imply
the root logger. Changing the types in split_kv to use str | None
fixes the problem, although a better fix might be to change
the log level handler to support "" or None.
  • Loading branch information
timj committed Jul 22, 2024
1 parent 1612e9b commit df5e712
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion python/lsst/daf/butler/cli/cliLog.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def _setLogLevel(cls, component: str | None, level: str) -> None:
Parameters
----------
component : `str` or None
The name of the log component or None for the default logger.
The name of the log component or `None` for the default logger.
The root logger can be specified either by an empty string or
with the special name ``.``.
level : `str`
Expand Down
2 changes: 1 addition & 1 deletion python/lsst/daf/butler/cli/opt/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def makeCollectionTypes(
)


def _config_split(*args: Any) -> dict[str, str]:
def _config_split(*args: Any) -> dict[str | None, str]:
# Config values might include commas so disable comma-splitting.
result = split_kv(*args, multiple=False)
assert isinstance(result, dict), "For mypy check that we get the expected result"
Expand Down
19 changes: 10 additions & 9 deletions python/lsst/daf/butler/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,10 @@ def split_kv(
separator: str = "=",
unseparated_okay: bool = False,
return_type: type[dict] | type[tuple] = dict,
default_key: str = "",
default_key: str | None = "",
reverse_kv: bool = False,
add_to_default: bool = False,
) -> dict[str, str] | tuple[tuple[str, str], ...]:
) -> dict[str | None, str] | tuple[tuple[str | None, str], ...]:
"""Process a tuple of values that are key-value pairs separated by a given
separator. Multiple pairs may be comma separated. Return a dictionary of
all the passed-in values.
Expand Down Expand Up @@ -401,8 +401,9 @@ def split_kv(
tuple will be 2-item tuple, the first item will be the value to the
left of the separator and the second item will be the value to the
right. By default `dict`.
default_key : `Any`
default_key : `str` or `None`
The key to use if a value is passed that is not a key-value pair.
`None` can imply no separator depending on how the results are handled.
(Passing values that are not key-value pairs requires
``unseparated_okay`` to be `True`).
reverse_kv : bool
Expand Down Expand Up @@ -453,26 +454,26 @@ def norm(val: str) -> str:

class RetDict:
def __init__(self) -> None:
self.ret: dict[str, str] = {}
self.ret: dict[str | None, str] = {}

def add(self, key: str, val: str) -> None:
def add(self, key: str | None, val: str) -> None:
if reverse_kv:
key, val = val, key
self.ret[key] = val

def get(self) -> dict[str, str]:
def get(self) -> dict[str | None, str]:
return self.ret

class RetTuple:
def __init__(self) -> None:
self.ret: list[tuple[str, str]] = []
self.ret: list[tuple[str | None, str]] = []

def add(self, key: str, val: str) -> None:
def add(self, key: str | None, val: str) -> None:
if reverse_kv:
key, val = val, key
self.ret.append((key, val))

def get(self) -> tuple[tuple[str, str], ...]:
def get(self) -> tuple[tuple[str | None, str], ...]:
return tuple(self.ret)

if separator in (",", " "):
Expand Down

0 comments on commit df5e712

Please sign in to comment.