Skip to content

Commit

Permalink
fix: support int in CompressionType.get
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravencentric committed Aug 18, 2024
1 parent 2fb8352 commit 2cae6bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/archivefile/_enums.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from enum import IntEnum
from typing import Literal
from typing import Literal, overload


class CompressionType(IntEnum):
Expand All @@ -25,11 +25,23 @@ class CompressionType(IntEnum):
This requires the [lzma](https://docs.python.org/3/library/lzma.html#module-lzma) module.
"""

@overload
@classmethod
def get(
cls,
key: str | int | None = None,
default: Literal["stored", "deflated", "bzip2", "lzma"] = "stored",
) -> CompressionType: ...

@overload
@classmethod
def get(cls, key: str | int | None = None, default: str | int = "stored") -> CompressionType: ...

@classmethod
def get(
cls,
key: str | int | None = None,
default: Literal["stored", "deflated", "bzip2", "lzma"] | str | int = "stored",
) -> CompressionType:
"""
Get the `CompressionType` by its name or number.
Expand All @@ -39,6 +51,8 @@ def get(
----------
key : str | int, optional
The key to retrieve.
default : Literal["stored", "deflated", "bzip2", "lzma"] | str | int, optional
The default value to return if the key is missing or invalid.
Returns
-------
Expand All @@ -54,6 +68,6 @@ def get(
return cls(key)

case _:
return cls[default.upper()]
return cls[default.upper()] if isinstance(default, str) else cls(default)
except (KeyError, ValueError):
return cls[default.upper()]
return cls[default.upper()] if isinstance(default, str) else cls(default)
5 changes: 5 additions & 0 deletions tests/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ def test_compression_type_enum() -> None:
assert CompressionType.get("non-existent-key") is CompressionType.STORED
assert CompressionType.get(999999999999, "bzip2") is CompressionType.BZIP2
assert CompressionType.get("non-existent-key", "lzma") is CompressionType.LZMA

assert CompressionType.get(999999999999) is CompressionType.STORED
assert CompressionType.get("non-existent-key") is CompressionType.STORED
assert CompressionType.get(999999999999, CompressionType.BZIP2) is CompressionType.BZIP2
assert CompressionType.get("non-existent-key", CompressionType.LZMA) is CompressionType.LZMA

0 comments on commit 2cae6bb

Please sign in to comment.