Skip to content

Commit

Permalink
review findings and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 committed Jul 28, 2024
1 parent 08de244 commit cf2df80
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 20 deletions.
24 changes: 24 additions & 0 deletions src/towncrier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,27 @@
"""
towncrier, a builder for your news files.
"""

from __future__ import annotations


__all__ = ["__version__"]


def __getattr__(name: str) -> str:
if name != "__version__":
raise AttributeError(f"module {__name__} has no attribute {name}")

import warnings

from ._version import __version__

warnings.warn(
"Accessing towncrier.__version__ is deprecated and will be "
"removed in a future release. Use importlib.metadata directly "
"to query for towncrier's packaging metadata.",
DeprecationWarning,
stacklevel=2,
)

return __version__
8 changes: 3 additions & 5 deletions src/towncrier/_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

from __future__ import annotations

import contextlib
import importlib.metadata as importlib_metadata
import sys

from importlib import import_module
from importlib.metadata import version as metadata_version
from types import ModuleType
from typing import Any


if sys.version_info >= (3, 10):
Expand Down Expand Up @@ -62,7 +62,7 @@ def get_version(package_dir: str, package: str) -> str:
Try to extract the version from the distribution version metadata that matches
`package`, then fall back to looking for the package in `package_dir`.
"""
version: Any
version: str

# First try to get the version from the package metadata.
if version := _get_metadata_version(package):
Expand Down Expand Up @@ -105,9 +105,7 @@ def get_project_name(package_dir: str, package: str) -> str:
module = _get_package(package_dir, package)
version = getattr(module, "__version__", None)
# Incremental has support for package names, try duck-typing it.
try:
with contextlib.suppress(AttributeError):
return str(version.package) # type: ignore
except AttributeError:
pass

return package.title()
3 changes: 1 addition & 2 deletions src/towncrier/_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@

import click

from click_default_group import DefaultGroup

from .build import _main as _build_cmd
from .check import _main as _check_cmd
from .click_default_group import DefaultGroup
from .create import _main as _create_cmd


Expand Down
14 changes: 6 additions & 8 deletions src/towncrier/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
Provides towncrier version information.
"""

from importlib.metadata import PackageNotFoundError, version
# For dev - 23.11.1.dev0
# For RC - 23.11.1.rc1
# For final - 23.11.1
# make sure to follow PEP440
__version__ = "23.11.1.dev0"


try:
_version = version("towncrier")
except PackageNotFoundError: # pragma: no cover
_version = "0.0.0.dev"

_hatchling_version = _version
_hatchling_version = __version__
__all__ = ["_hatchling_version"]
18 changes: 14 additions & 4 deletions src/towncrier/test/test_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@


class TestPackaging(TestCase):
def no_version_attr(self):
def test_version_attr(self):
"""
towncrier.__version__ was deprecated, now no longer exists.
towncrier.__version__ was deprecated, but still exists for now.
"""

with self.assertRaises(AttributeError):
towncrier.__version__
def access__version():
return towncrier.__version__

expected_warning = (
"Accessing towncrier.__version__ is deprecated and will be "
"removed in a future release. Use importlib.metadata directly "
"to query for towncrier's packaging metadata."
)

self.assertWarns(
DeprecationWarning, expected_warning, __file__, access__version
)
2 changes: 1 addition & 1 deletion src/towncrier/test/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_missing_version(self):
get_version(tmp_dir, "missing")

self.assertEqual(
("Package not installed and no missing.__version__ found",),
("No __version__ or metadata version info for the 'missing' package.",),
e.exception.args,
)

Expand Down

0 comments on commit cf2df80

Please sign in to comment.