Repository: mozman/ezdxf

Issue:
`ezdxf.units.unit_name` always returns `unitless` in some environment
Thank you for maintaining this awesome package!


**Describe the bug**
`ezdxf.units.unit_name` should return the string of the unit name for a given integer of INSUNITS, but in some environment it always returns `"unitless"` regardless of an input.

I guess this happens in __Python 3.11__ or later because since then the behaviour of `IntEnum.__str__` has been changed as follows:

https://docs.python.org/3/library/enum.html#enum.IntEnum
> Changed in version 3.11: [__str__()](https://docs.python.org/3/reference/datamodel.html#object.__str__) is now int.__str__() to better support the replacement of existing constants use-case. [__format__()](https://docs.python.org/3/reference/datamodel.html#object.__format__) was already int.__format__() for that same reason.

(I think [the implementation](
https://github.com/mozman/ezdxf/blob/c0a649d263724254b9c2176ee5c9280d81a1b163/src/ezdxf/units.py#L201-L206) of `unit_name` can just use `InsertUnits(enum).name` instead of `str()`.)


**To Reproduce**

```
print(ezdxf.units.unit_name(0))  # -> unitless
print(ezdxf.units.unit_name(1))  # -> unitless
```

As mentioned above, this should be reproduced in Python 3.11 (or later, but I haven't checked that).

**Expected behavior**

```
print(ezdxf.units.unit_name(0))  # -> Unitless
print(ezdxf.units.unit_name(1))  # -> Inches
```

Discussion:

Solution patch:
diff --git a/src/ezdxf/units.py b/src/ezdxf/units.py
index 8218670fb..a82ae0fde 100644
--- a/src/ezdxf/units.py
+++ b/src/ezdxf/units.py
@@ -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"
 
 


Test patch:
diff --git a/tests/test_05_tools/test_500_units.py b/tests/test_05_tools/test_500_units.py
index 4b6760053..1010f9edf 100644
--- a/tests/test_05_tools/test_500_units.py
+++ b/tests/test_05_tools/test_500_units.py
@@ -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
 
 
@@ -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"