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

Fix units.unit_name to work on Python 3.11 or later #1132

Merged
merged 3 commits into from
Jul 28, 2024
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
4 changes: 2 additions & 2 deletions src/ezdxf/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ def conversion_factor(
def unit_name(enum: int) -> str:
"""Returns the name of the unit enum."""
try:
return str(InsertUnits(enum)).split(".")[1]
except (ValueError, IndexError):
return InsertUnits(enum).name
except ValueError:
return "unitless"


Expand Down
11 changes: 10 additions & 1 deletion tests/test_05_tools/test_500_units.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2019-2020 Manfred Moitzi
# License: MIT License
import pytest
from ezdxf.units import DrawingUnits, PaperSpaceUnits, conversion_factor
from ezdxf.units import DrawingUnits, PaperSpaceUnits, conversion_factor, unit_name
from math import isclose


Expand Down Expand Up @@ -159,3 +159,12 @@ def test_conversion_type_error(source, target):
def test_conversion_invalid_units():
with pytest.raises(ValueError):
conversion_factor(25, 6)


def test_unit_name_valid_input():
assert unit_name(0) == "Unitless"
assert unit_name(1) == "Inches"


def test_unit_name_invalid_input():
assert unit_name(-1) == "unitless"
Loading