poetry-core icon indicating copy to clipboard operation
poetry-core copied to clipboard

Refactor Ideas

Open Anselmoo opened this issue 2 years ago • 18 comments

Resolves: python-poetry#

Based on the Sonar-Cloud's recommendation concerning code quality of nested if-structures, I propose some tiny changes to reduce the complexity.

https://sonarcloud.io/project/issues?id=poetry-core&open=AXnJYepALR9UFIruhRJy&resolved=false&types=CODE_SMELL

  • [ ] Added tests for changed code.
  • [ ] Updated documentation for changed code.

Anselmoo avatar Feb 01 '22 20:02 Anselmoo

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
1.9% 1.9% Duplication

sonarcloud[bot] avatar Feb 01 '22 20:02 sonarcloud[bot]

Much of this appears to be in vendored code, I don't expect this project wants to be making changes there. Perhaps you could take these changes upstream, if you feel motivated to do so.

dimbleby avatar Feb 02 '22 00:02 dimbleby

Perhaps you could take these changes upstream, if you feel motivated to do so.

@dimbleby sorry, can you explain me a little bit more? Thx

Anselmoo avatar Feb 02 '22 09:02 Anselmoo

Most of the changes currently in this MR are in third party code, vendored into this repository. It would make more sense to offer those changes to the original projects.

The changes that you've made to poetry-core's own code are fair game!

dimbleby avatar Feb 02 '22 09:02 dimbleby

Thx for clarification, I will then take a look from time to time

Anselmoo avatar Feb 02 '22 09:02 Anselmoo

@Anselmoo Any chance you'll be able to get back to this? All the changes in the _vendor folder needs to be dropped, and I'd like to reduce the noise so we can get this reviewed.

neersighted avatar Jun 04 '22 23:06 neersighted

@Anselmoo Any chance you'll be able to get back to this? All the changes in the _vendor folder needs to be dropped, and I'd like to reduce the noise so we can get this reviewed.

@neersighted thx for letting me know. I wasn't sure if that's still interesting.

If I am correct, the vendor code should be untouched and the rest be checked, again? If so, I will take care. thx

Anselmoo avatar Jun 05 '22 05:06 Anselmoo

@Anselmoo Any chance you'll be able to get back to this? All the changes in the _vendor folder needs to be dropped, and I'd like to reduce the noise so we can get this reviewed.

@neersighted thx for letting me know. I wasn't sure if that's still interesting.

If I am correct, the vendor code should be untouched and the rest be checked, again? If so, I will take care. thx

That is correct!

neersighted avatar Jun 05 '22 06:06 neersighted

Sorry, @neersighted ~~it seems to be that the rebase of my PR to the current main wasn't successful.~~

Looks better!

It might a good idea to add mypy-module to https://github.com/python-poetry/poetry-core/blob/main/.pre-commit-config.yaml ??

Anselmoo avatar Jun 05 '22 07:06 Anselmoo

In case of a permanent check of possible refactors, you can actually use https://github.com/sourcery-ai/sourcery as part of the CI/CD.

ignore: ["src/poetry/core/_vendor"]
refactor:
  skip: ["extract-method"]
  python_version: '3.7'

Anselmoo avatar Jun 05 '22 09:06 Anselmoo

The changes in e932cdab157189088b357849f080850d5e457feb make things worse, not better.

The type signatures were correct before, and scattering Any all over the place simply makes them less useful.

Edit: I see that most of them relate to NotImplemented. This is a special case that mypy was capable of understanding before the refactoring.

dimbleby avatar Jun 05 '22 09:06 dimbleby

The changes in e932cda make things worse, not better.

The type signatures were correct before, and scattering Any all over the place simply makes them less useful.

Edit: I see that most of them relate to NotImplemented. This is a special case that mypy was capable of understanding before the refactoring.

That's exactly the point, I just wanna write it:

https://github.com/python-poetry/poetry-core/blob/3bf7ad0f1dc2b1a5cabe4bb58cf7e1c6d832870d/src/poetry/core/packages/dependency_group.py#L47-L53

Unfortunately, NotImplemented is type of Any and types.NotImplementedType is only available for python >=3.10.

https://docs.python.org/3/library/types.html#types.NotImplementedType

Anselmoo avatar Jun 05 '22 10:06 Anselmoo

I don't understand your point. That's exactly the current implementation on master, and it typechecks just fine without an Any annotation?

dimbleby avatar Jun 05 '22 10:06 dimbleby

I don't understand your point. That's exactly the current implementation on master, and it typechecks just fine without an Any annotation?

Indeed, it's a little bit wired ...

image

https://github.com/python-poetry/poetry-core/pull/279/commits/6e3cf6fc039cc3c88edd06e3e674a0cde65f5630

I was running in poetry-main, in code spaces, and in the poetry shell, it's also fine. I am wondering, why the previous test fails?

Anselmoo avatar Jun 05 '22 10:06 Anselmoo

As I say, I think that mypy has a special case for NotImplemented and the refactoring has disturbed the balance.

Suggest just put that code back as it was and don't worry about it.

(While recognising that such things can be a matter of taste: to my mind the rearrangements in __eq__ etc aren't even an improvement!)

dimbleby avatar Jun 05 '22 10:06 dimbleby

As I say, I think that mypy has a special case for NotImplemented and the refactoring has disturbed the balance.

Suggest just put that code back as it was and don't worry about it.

(While recognising that such things can be a matter of taste: to my mind the rearrangements in __eq__ etc aren't even an improvement!)

Ic, thx @dimbleby for the clarification!

It took me a little bit too long to get your point, sorry

Anselmoo avatar Jun 05 '22 10:06 Anselmoo

@neersighted can you take a look later, please?

Anselmoo avatar Jul 05 '22 06:07 Anselmoo

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

sonarcloud[bot] avatar Jul 09 '22 12:07 sonarcloud[bot]

Since it's been almost a year since this PR was last touched and the list of conflicts is quite extensive, I am going to close it. If anyone wants to pick up some of the changes from this PR, feel free to do so and open new PR.

Secrus avatar Jun 05 '23 16:06 Secrus