Repository: tefra/xsdata
Pull request #517 (2021-06-05 14:58:57)

Sort classes by name before renaming
Specially in netex collection it's common to have base types that have an underscore prefix or a suffix, when it's time to resolve these conflict the handler assigns auto increment suffixes by following whatever order the classes arrive in the handler.

So for example  the class type `ParkingSpace` is renamed to `ParkingSpace2` and the type `_ParkingSpace` to `ParkingSpace1`, which seems a bit unfair, so sort the classes by name before assigning new names



sonarcloud[bot]: Kudos, SonarCloud Quality Gate passed!

[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug.png' alt='Bug' width='16' height='16' />](https://sonarcloud.io/project/issues?id=tefra_xsdata&pullRequest=517&resolved=false&types=BUG) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=tefra_xsdata&pullRequest=517&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=tefra_xsdata&pullRequest=517&resolved=false&types=BUG)  
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability.png' alt='Vulnerability' width='16' height='16' />](https://sonarcloud.io/project/issues?id=tefra_xsdata&pullRequest=517&resolved=false&types=VULNERABILITY) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=tefra_xsdata&pullRequest=517&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=tefra_xsdata&pullRequest=517&resolved=false&types=VULNERABILITY)  
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot.png' alt='Security Hotspot' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=tefra_xsdata&pullRequest=517&resolved=false&types=SECURITY_HOTSPOT) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/security_hotspots?id=tefra_xsdata&pullRequest=517&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=tefra_xsdata&pullRequest=517&resolved=false&types=SECURITY_HOTSPOT)  
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell.png' alt='Code Smell' width='16' height='16' />](https://sonarcloud.io/project/issues?id=tefra_xsdata&pullRequest=517&resolved=false&types=CODE_SMELL) [<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A.png' alt='A' width='16' height='16' />](https://sonarcloud.io/project/issues?id=tefra_xsdata&pullRequest=517&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=tefra_xsdata&pullRequest=517&resolved=false&types=CODE_SMELL)

[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo.png' alt='No Coverage information' width='16' height='16' />](https://sonarcloud.io/component_measures?id=tefra_xsdata&pullRequest=517) No Coverage information  
[<img src='https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3.png' alt='0.0%' width='16' height='16' />](https://sonarcloud.io/component_measures?id=tefra_xsdata&pullRequest=517&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=tefra_xsdata&pullRequest=517&metric=new_duplicated_lines_density&view=list)


codecov[bot]: # [Codecov](https://codecov.io/gh/tefra/xsdata/pull/517?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou) Report
> Merging [#517](https://codecov.io/gh/tefra/xsdata/pull/517?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou) (5187f98) into [master](https://codecov.io/gh/tefra/xsdata/commit/1c65024eaaf607f565125daf4c9f4330784fdb97?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou) (1c65024) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/tefra/xsdata/pull/517/graphs/tree.svg?width=650&height=150&src=pr&token=YzDDLtywvl&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou)](https://codecov.io/gh/tefra/xsdata/pull/517?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou)

```diff
@@            Coverage Diff            @@
##            master      #517   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           87        87           
  Lines         7512      7514    +2     
  Branches      1313      1312    -1     
=========================================
+ Hits          7512      7514    +2     
```


| [Impacted Files](https://codecov.io/gh/tefra/xsdata/pull/517?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou) | Coverage Δ | |
|---|---|---|
| [xsdata/codegen/handlers/attribute\_default\_value.py](https://codecov.io/gh/tefra/xsdata/pull/517/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou#diff-eHNkYXRhL2NvZGVnZW4vaGFuZGxlcnMvYXR0cmlidXRlX2RlZmF1bHRfdmFsdWUucHk=) | `100.00% <100.00%> (ø)` | |
| [xsdata/codegen/handlers/class\_name\_conflict.py](https://codecov.io/gh/tefra/xsdata/pull/517/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou#diff-eHNkYXRhL2NvZGVnZW4vaGFuZGxlcnMvY2xhc3NfbmFtZV9jb25mbGljdC5weQ==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/tefra/xsdata/pull/517?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/tefra/xsdata/pull/517?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou). Last update [1c65024...5187f98](https://codecov.io/gh/tefra/xsdata/pull/517?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Chris+Tsou).


Patch:
diff --git a/xsdata/codegen/handlers/attribute_default_value.py b/xsdata/codegen/handlers/attribute_default_value.py
index d6975b91..d757d79f 100644
--- a/xsdata/codegen/handlers/attribute_default_value.py
+++ b/xsdata/codegen/handlers/attribute_default_value.py
@@ -1,3 +1,4 @@
+from operator import attrgetter
 from typing import Optional
 
 from xsdata.codegen.mixins import RelativeHandlerInterface
@@ -85,6 +86,5 @@ class AttributeDefaultValueHandler(RelativeHandlerInterface):
         if attr_type.native:
             return None
 
-        return self.container.find(
-            attr_type.qname, condition=lambda x: x.is_enumeration
-        )
+        is_enumeration = attrgetter("is_enumeration")
+        return self.container.find(attr_type.qname, condition=is_enumeration)
diff --git a/xsdata/codegen/handlers/class_name_conflict.py b/xsdata/codegen/handlers/class_name_conflict.py
index f075e197..9acbf95a 100644
--- a/xsdata/codegen/handlers/class_name_conflict.py
+++ b/xsdata/codegen/handlers/class_name_conflict.py
@@ -40,7 +40,7 @@ class ClassNameConflictHandler(ContainerHandlerInterface):
         the list.
         """
         total_elements = sum(x.is_element for x in classes)
-        for target in classes:
+        for target in sorted(classes, key=operator.attrgetter("name")):
             if not target.is_element or total_elements > 1:
                 self.rename_class(target, use_name)
 


Test patch:
diff --git a/tests/codegen/handlers/test_class_name_conflict.py b/tests/codegen/handlers/test_class_name_conflict.py
index 7b04923f..6fa67aaa 100644
--- a/tests/codegen/handlers/test_class_name_conflict.py
+++ b/tests/codegen/handlers/test_class_name_conflict.py
@@ -77,11 +77,11 @@ class ClassNameConflictHandlerTests(FactoryTestCase):
 
         mock_rename_class.assert_has_calls(
             [
-                mock.call(classes[0], False),
                 mock.call(classes[1], False),
+                mock.call(classes[0], False),
                 mock.call(classes[2], False),
-                mock.call(classes[0], True),
                 mock.call(classes[1], True),
+                mock.call(classes[0], True),
                 mock.call(classes[2], True),
             ]
         )