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

#432 Use importlib.metadata.version to get the version #502

Merged
merged 22 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c933cbb
#432 An initial attempt
agriyakhetarpal Apr 29, 2023
89c3232
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 29, 2023
d8e281f
Merge branch 'trunk' into 432-poetry-version
agriyakhetarpal May 2, 2023
3aff9fb
Merge branch 'trunk' into 432-poetry-version
adiroiban Jun 4, 2023
ff05eed
Merge branch 'trunk' into 432-poetry-version
adiroiban Jul 17, 2023
69b197a
Merge branch 'trunk' into 432-poetry-version
adiroiban Apr 28, 2024
c2a23df
Get metadata version from packages_distributions
SmileyChris May 7, 2024
2af8e80
Fix dependency format
SmileyChris May 21, 2024
6b85972
Safer check in _get_metadata_version
SmileyChris May 21, 2024
819d160
Update test to match new "no version found" exception text
SmileyChris May 21, 2024
7f1d8ce
logic style nitpick
SmileyChris May 21, 2024
1f28954
Mypy fixes
SmileyChris May 21, 2024
770eb48
Fix incremental test
SmileyChris May 21, 2024
9bbcb70
Add test to check version from metadata
SmileyChris May 22, 2024
85a769e
Remove a redundant try/except
SmileyChris May 22, 2024
30fe7ed
Looser pin for importlib-metadata
SmileyChris May 22, 2024
6ec518d
Use a pyupgrade compatible format for the importlib_metadata fallback…
SmileyChris May 22, 2024
d5e0a60
Improve comments for the version-related methods
SmileyChris May 22, 2024
f02a26a
Small tweak to the version error message
SmileyChris May 22, 2024
8050a0b
Revert missing version name change in test
SmileyChris May 22, 2024
5bee9d4
Add a newsfragment
SmileyChris May 22, 2024
783e122
Merge branch 'trunk' into 432-poetry-version
SmileyChris May 22, 2024
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 pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ requires-python = ">=3.8"
dependencies = [
"click",
"importlib-resources>=5; python_version<'3.10'",
"importlib-metadata==4.6.*; python_version<'3.10'",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this needs a comment to explain why we are pinning on 4.6 version.

Why not use the latest version ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better pin would be >=4.6 I guess (this is the version that had parity with Python 3.10).

"incremental",
"jinja2",
"tomli; python_version<'3.11'",
Expand Down
29 changes: 27 additions & 2 deletions src/towncrier/_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
Responsible for getting the version and name from a project.
"""


from __future__ import annotations

import sys

from importlib import import_module
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as metadata_version
from types import ModuleType

from incremental import Version as IncrementalVersion


try:
from importlib.metadata import packages_distributions
except ImportError:
from importlib_metadata import packages_distributions


def _get_package(package_dir: str, package: str) -> ModuleType:
try:
module = import_module(package)
Expand All @@ -37,13 +44,31 @@ def _get_package(package_dir: str, package: str) -> ModuleType:
return module


def _get_metadata_version(package: str) -> str | None:
distributions = packages_distributions()
distribution_names = distributions.get(package)
SmileyChris marked this conversation as resolved.
Show resolved Hide resolved
if not distribution_names or not len(distribution_names) == 1:
# We can only determine the version if there is exactly one matching distribution.
return None
try:
return metadata_version(distribution_names[0])
except PackageNotFoundError:
return None


def get_version(package_dir: str, package: str) -> str:
SmileyChris marked this conversation as resolved.
Show resolved Hide resolved
# First try to get the version from the package metadata.
if version := _get_metadata_version(package):
return version

module = _get_package(package_dir, package)

version = getattr(module, "__version__", None)

if not version:
raise Exception("No __version__, I don't know how else to look")
raise Exception(
f"No __version__ or metadata version info for the {package} package."
SmileyChris marked this conversation as resolved.
Show resolved Hide resolved
)

if isinstance(version, str):
return version.strip()
Expand Down
Loading