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

Fix #6171 #6247

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/6171.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed package sorting when installing a package with extras.
22 changes: 17 additions & 5 deletions pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from urllib.parse import unquote, urljoin

from pipenv.utils.constants import VCS_LIST
from pipenv.vendor.tomlkit.items import SingleKey, Table

try:
import tomllib as toml
Expand Down Expand Up @@ -1107,12 +1108,23 @@ def get_package_name_in_pipfile(self, package_name, category):
return name
return None

def _sort_category(self, category):
# toml tables won't maintain sorted dictionary order
# so construct the table in the order that we need
def _sort_category(self, category) -> Table:
# copy table or create table from dict-like object
table = tomlkit.table()
for key, value in sorted(category.items()):
table.add(key, value)
if isinstance(category, Table):
table.update(category.value)
else:
table.update(category)

# sort the table internally
table._value._body.sort(key=lambda t: t[0] and t[0].key or "")
for index, (key, _) in enumerate(table._value._body):
assert isinstance(key, SingleKey)
indices = table._value._map[key]
if isinstance(indices, tuple):
table._value._map[key] = (index,) + indices[1:]
else:
table._value._map[key] = index

return table

Expand Down
31 changes: 19 additions & 12 deletions pipenv/utils/toml.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Union

from pipenv.vendor.plette.models import Package, PackageCollection
from pipenv.vendor.tomlkit.container import Container
from pipenv.vendor.tomlkit.container import Container, OutOfOrderTableProxy
from pipenv.vendor.tomlkit.items import AoT, Array, Bool, InlineTable, Item, String, Table
from pipenv.vendor.tomlkit.toml_document import TOMLDocument

try:
import tomllib as toml
Expand Down Expand Up @@ -33,26 +34,32 @@ def cleanup_toml(tml):
return toml


def convert_toml_outline_tables(parsed, project):
def convert_toml_outline_tables(parsed: TOMLDocument, project) -> TOMLDocument:
"""Converts all outline tables to inline tables."""

def convert_tomlkit_table(section):
result = section.copy()
if isinstance(section, tomlkit.items.Table):
result: Table = tomlkit.table()
if isinstance(section, Table):
body = section.value._body
elif isinstance(section, tomlkit.container.OutOfOrderTableProxy):
elif isinstance(section, OutOfOrderTableProxy):
body = section._internal_container._body
else:
body = section._body
assert not hasattr(section, "_body")
body = section

index: int = 0
for key, value in body:
if not key:
continue
if hasattr(value, "keys") and not isinstance(
value, tomlkit.items.InlineTable
):
if hasattr(value, "keys") and not isinstance(value, InlineTable):
table = tomlkit.inline_table()
table.update(value.value)
result[key.key] = table
key.sep = " = " # add separator because it did not exist before
result.append(key, table)
else:
result.append(key, value)
index += 1

return result

def convert_toml_table(section):
Expand All @@ -66,10 +73,10 @@ def convert_toml_table(section):
result[package] = table
return result

is_tomlkit_parsed = isinstance(parsed, tomlkit.container.Container)
is_tomlkit_parsed = isinstance(parsed, Container)
for section in project.get_package_categories():
table_data = parsed.get(section, {})
if not table_data:
if table_data is None:
continue
if is_tomlkit_parsed:
result = convert_tomlkit_table(table_data)
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
@pytest.fixture()
def project():
from pipenv.project import Project

return Project()
19 changes: 8 additions & 11 deletions tests/fixtures/cython-import-package/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,31 @@
import Cython.Distutils



ROOT = os.path.dirname(__file__)

PACKAGE_NAME = 'cython_import_package'
PACKAGE_NAME = "cython_import_package"

VERSION = None

with open(os.path.join(ROOT, 'src', PACKAGE_NAME.replace("-", "_"), '__init__.py')) as f:
with open(os.path.join(ROOT, "src", PACKAGE_NAME.replace("-", "_"), "__init__.py")) as f:
for line in f:
if line.startswith('__version__ = '):
VERSION = ast.literal_eval(line[len('__version__ = '):].strip())
if line.startswith("__version__ = "):
VERSION = ast.literal_eval(line[len("__version__ = ") :].strip())
break
if VERSION is None:
raise OSError('failed to read version')
raise OSError("failed to read version")


# Put everything in setup.cfg, except those that don't actually work?
setup(
# These really don't work.
package_dir={'': 'src'},
packages=find_packages('src'),

package_dir={"": "src"},
packages=find_packages("src"),
# I don't know how to specify an empty key in setup.cfg.
package_data={
'': ['LICENSE*', 'README*'],
"": ["LICENSE*", "README*"],
},
setup_requires=["setuptools_scm", "cython"],

# I need this to be dynamic.
version=VERSION,
)
94 changes: 50 additions & 44 deletions tests/fixtures/fake-package/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
#
import os
import sys

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PACKAGE_DIR = os.path.join(ROOT, "src/fake_package")
sys.path.insert(0, PACKAGE_DIR)


# -- Project information -----------------------------------------------------

project = 'fake_package'
copyright = '2019, Dan Ryan <[email protected]>'
author = 'Dan Ryan <[email protected]>'
project = "fake_package"
copyright = "2019, Dan Ryan <[email protected]>"
author = "Dan Ryan <[email protected]>"

# The short X.Y version
version = '0.0'
version = "0.0"
# The full version, including alpha/beta/rc tags
release = '0.0.0.dev0'
release = "0.0.0.dev0"


# -- General configuration ---------------------------------------------------
Expand All @@ -40,39 +41,39 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.viewcode',
'sphinx.ext.todo',
'sphinx.ext.intersphinx',
'sphinx.ext.autosummary'
"sphinx.ext.autodoc",
"sphinx.ext.viewcode",
"sphinx.ext.todo",
"sphinx.ext.intersphinx",
"sphinx.ext.autosummary",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'
source_suffix = ".rst"

# The master toctree document.
master_doc = 'index'
master_doc = "index"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = 'en'
language = "en"

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path .
exclude_patterns = ['_build', '_man', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "_man", "Thumbs.db", ".DS_Store"]

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
pygments_style = "sphinx"
autosummary_generate = True


Expand All @@ -81,7 +82,7 @@
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_theme = "sphinx_rtd_theme"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
Expand All @@ -92,7 +93,7 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
Expand All @@ -108,22 +109,22 @@
# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'fake_packagedoc'
htmlhelp_basename = "fake_packagedoc"
extlinks = {
'issue': ('https://github.com/sarugaku/fake_package/issues/%s', '#'),
'pull': ('https://github.com/sarugaku/fake_package/pull/%s', 'PR #'),
"issue": ("https://github.com/sarugaku/fake_package/issues/%s", "#"),
"pull": ("https://github.com/sarugaku/fake_package/pull/%s", "PR #"),
}
html_theme_options = {
'display_version': True,
'prev_next_buttons_location': 'bottom',
'style_external_links': True,
'vcs_pageview_mode': '',
"display_version": True,
"prev_next_buttons_location": "bottom",
"style_external_links": True,
"vcs_pageview_mode": "",
# Toc options
'collapse_navigation': True,
'sticky_navigation': True,
'navigation_depth': 4,
'includehidden': True,
'titles_only': False
"collapse_navigation": True,
"sticky_navigation": True,
"navigation_depth": 4,
"includehidden": True,
"titles_only": False,
}

# -- Options for LaTeX output ------------------------------------------------
Expand All @@ -132,15 +133,12 @@
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',

# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
Expand All @@ -150,19 +148,21 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'fake_package.tex', 'fake_package Documentation',
'Dan Ryan \\textless{}[email protected]\\textgreater{}', 'manual'),
(
master_doc,
"fake_package.tex",
"fake_package Documentation",
"Dan Ryan \\textless{}[email protected]\\textgreater{}",
"manual",
),
]


# -- Options for manual page output ------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'fake_package', 'fake_package Documentation',
[author], 1)
]
man_pages = [(master_doc, "fake_package", "fake_package Documentation", [author], 1)]


# -- Options for Texinfo output ----------------------------------------------
Expand All @@ -171,9 +171,15 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'fake_package', 'fake_package Documentation',
author, 'fake_package', 'A fake python package.',
'Miscellaneous'),
(
master_doc,
"fake_package",
"fake_package Documentation",
author,
"fake_package",
"A fake python package.",
"Miscellaneous",
),
]


Expand All @@ -195,7 +201,7 @@
# epub_uid = ''

# A list of files that should not be packed into the epub file.
epub_exclude_files = ['search.html']
epub_exclude_files = ["search.html"]


# -- Extension configuration -------------------------------------------------
Expand All @@ -204,4 +210,4 @@

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True
intersphinx_mapping = {'python': ('https://docs.python.org/3', None)}
intersphinx_mapping = {"python": ("https://docs.python.org/3", None)}
Loading
Loading