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

feat: use SortedSet in model to improve reproducibility #235

Merged
merged 15 commits into from
Jun 10, 2022

Conversation

RodneyRichardson
Copy link
Contributor

Added __lt__() to all model classes used in SortedSet, with tests
Explicitly declared Enums as (str, Enum) to allow sorting
Added dependency to sortedcollections package
Added ComparableTuple to make sorting compound objects easier

Signed-off-by: Rodney Richardson rodney.richardson@cambridgeconsultants.com

Added `__lt__()` to all model classes used in SortedSet, with tests
Explicitly declared Enums as (str, Enum) to allow sorting
Added dependency to sortedcollections package

Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
@RodneyRichardson RodneyRichardson requested a review from a team as a code owner May 27, 2022 13:48
Remove unused imports and trailing whitespace.
Sort usings in pyi file.

Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
@RodneyRichardson
Copy link
Contributor Author

RodneyRichardson commented May 27, 2022

@RodneyRichardson besides those lift findings, please fix the findings of mypy and other tools.

I've fixed all those, I think. I have an outstanding mypy error I don't know how to resolve:
https://github.com/CycloneDX/cyclonedx-python-lib/runs/6628435343?check_suite_focus=true

cyclonedx/model/__init__.py:62: error: Missing type parameters for generic type
"tuple"  [type-arg]
    class ComparableTuple(tuple):

I've tried replacing tuple with tuple[T,...], but I get a false positive about unreachable code. Any thoughts?

I'll try to look at the other lift issues - some are real, some are cosmetic (e.g. parameter names on overrides)

Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
@RodneyRichardson
Copy link
Contributor Author

@jkowalleck Ah - it also seems to be failing on python 3.8 and earlier! Any thgouhts?

File "/home/runner/work/cyclonedx-python-lib/cyclonedx-python-lib/cyclonedx/model/__init__.py", line 437, in <module>
    class ExternalReference:
  File "/home/runner/work/cyclonedx-python-lib/cyclonedx-python-lib/cyclonedx/model/__init__.py", line 499, in ExternalReference
    def hashes(self) -> SortedSet[HashType]:
TypeError: 'ABCMeta' object is not subscriptable

@jkowalleck
Copy link
Member

jkowalleck commented May 27, 2022

@RodneyRichardson

re:

cyclonedx/model/init.py:62: error: Missing type parameters for generic type "tuple" [type-arg]
class ComparableTuple(tuple):

the typing.Tuple might help.

from typing import Tuple

class Foo(Tuple[TypeOfItem1, and so on ]): 
  ...

regarding SortedSet and typing i cannot help.
My take on the topic would have implemented __lt__() in each data model (like so), and used sorted() when it came to iterating in the normalizer/serializer (like so).

Change tuple -> Tuple
Fix Diff initialization
Add sorting to AttachedText

Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
@RodneyRichardson
Copy link
Contributor Author

My take on the topic would have implemented __lt__() in each data model (like so), and used sorted() when it came to iterating in the normalizer/serializer (like so).

That was my initial implementation, and it was OK for the XML serialization, but the JSON serialization doesn't explicitly iterate the collections when serializing so it didn't work. This approach is more consistent across serializers.

Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
Signed-off-by: Rodney Richardson <rodney.richardson@cambridgeconsultants.com>
@RodneyRichardson
Copy link
Contributor Author

@jkowalleck I have resolved all issues, and and CI jobs are passing now. This is ready for review/merge.

FYI: To solve the type hint issues in python < 3.8, I needed to put SortedSet[T] in quotes.

@jkowalleck
Copy link
Member

Thanks for the feature, @RodneyRichardson

will review the PR when i have time.
Also i want to see what @madpah has to say.

This means, that the PR will take it's time.

@jkowalleck jkowalleck changed the title Use SortedSet in model to improve reproducibility feat: use SortedSet in model to improve reproducibility Jun 2, 2022
@jkowalleck
Copy link
Member

jkowalleck commented Jun 2, 2022

bumped mypy and several other dependencies,
in the hope this might fix possible StaticCodeAnalysis false-positives.

could you rebase onto latest "main" branch, @RodneyRichardson ?

@RodneyRichardson
Copy link
Contributor Author

could you rebase onto latest "main" branch, @RodneyRichardson ?

I have merged main into the PR/branch (just clicking the button in Github). Is this sufficient? Or do you need me to rebase the entire branch into a single commit? (I've not rebased on Github before).

@jkowalleck
Copy link
Member

jkowalleck commented Jun 6, 2022

could you rebase onto latest "main" branch, @RodneyRichardson ?

I have merged main into the PR/branch (just clicking the button in Github). Is this sufficient? Or do you need me to rebase the entire branch into a single commit? (I've not rebased on Github before).

no, thats fine.
The PR will be squashed by us on approval eventually.

@madpah
Copy link
Collaborator

madpah commented Jun 10, 2022

@RodneyRichardson - many thanks for your great contribution here. All looks good to me, so we'll get this merged imminently!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants