Skip to content

Commit

Permalink
Merge pull request #1132 from privet-kitty/feature/fix-unit-name
Browse files Browse the repository at this point in the history
Fix units.unit_name to work on Python 3.11 or later
  • Loading branch information
mozman authored Jul 28, 2024
2 parents c0a649d + f4c68bb commit ad0009b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
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"

0 comments on commit ad0009b

Please sign in to comment.