-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Introduce dependency sorting #3996
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
96e0fc5
command/add: introduce dependency sorting
br3ndonland c377365
tests: prefer actual-expected to expected-actual
br3ndonland c45dd9d
docs: update dependency sorting docs after review
br3ndonland e0b3946
command/init: introduce dependency sorting
br3ndonland 0d123d8
command/init: revise dependency sorting
br3ndonland 5e9f7da
command/add: rename dependency group to main
br3ndonland e9a353e
tests: use stable django-pendulum in sorting tests
br3ndonland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import sys | ||
|
||
from collections import OrderedDict | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
from typing import Any | ||
|
@@ -73,6 +74,7 @@ def handle(self) -> int: | |
from poetry.core.pyproject.toml import PyProjectTOML | ||
from poetry.core.vcs.git import GitConfig | ||
|
||
from poetry.config.config import Config | ||
from poetry.layouts import layout | ||
from poetry.utils.env import SystemEnv | ||
|
||
|
@@ -93,6 +95,7 @@ def handle(self) -> int: | |
) | ||
return 1 | ||
|
||
config = Config() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to read the global config, instead of adding a new flag to |
||
vcs_config = GitConfig() | ||
|
||
if self.io.is_interactive(): | ||
|
@@ -208,6 +211,10 @@ def handle(self) -> int: | |
if self.io.is_interactive(): | ||
self.line("") | ||
|
||
if config.get("dependencies.sort") is True: | ||
requirements = OrderedDict(sorted(requirements.items())) | ||
dev_requirements = OrderedDict(sorted(dev_requirements.items())) | ||
|
||
layout_ = layout("standard")( | ||
name, | ||
version, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more issue here: casting
section
to dictionary causes loss of comments in project file. That might be critical for some projects. Please, try to fix the issueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If comments were preserved, where would they go? We could probably preserve inline comments, but single line comments would be more difficult.
The most common use case for comments that I have seen is breaking up dependencies into subsections, like this:
If the dependencies were sorted, where would the comments go? It would probably end up like this:
A more effective approach might be to remove the comments and separate these commented sections into Poetry 1.2 dependency groups.
I'm happy to try preserving comments if there is a suggested data structure from TOML Kit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also comments might be used to temporarily disable a dependency or explain a dependency directly below the comment.
In these cases, it's quite important to keep those comments. I think it would be good to make the comments stick to the next dependency line:
should turn into
Your example is about a case that can be resolved using poetry functional, as you mentioned. Users should be responsible for proper use of Poetry. So I would keep comments in this case as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how to use tomlkit properly. But I can just share my thoughts.
It seems comment lines are stored in
section.value.body
list withNone
as a key andComment(..)
as a value. You can't get an access to the comment lines viasection.items()
method. I didn't find any other way to access to the comments butsection.value.body
. And I don't see methods to remove comment lines from the section (neithersection.remove()
norsection.clear()
don't remove the comments). So, you should re-create an entire section and append sorted lines into it, or try to sortsection.value.body
in-place.