-
Notifications
You must be signed in to change notification settings - Fork 128
Make configuration a proper class, push settings & check coverage to 100% #429
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
57b592a
Make configuration a class, fix a few type annotations
hynek 46a7f51
Add test for running towncrier from a diff dir
hynek 194f72f
Add test to verify the location of the config doesn't matter
hynek fe12df0
Add news fragment
hynek 48f1fad
Leave temporary dir before cleanup
hynek b657909
Eliminate unnecessary variables
hynek 89217a1
Organize test helpers
hynek a777cae
Add test for fragments directory configuration
hynek 62b3c9c
Use trial's clean instead of context mgr
hynek 1f77202
Update src/towncrier/build.py
hynek 4f29b12
Add type hints to test helpers for documentation
hynek 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,19 +7,49 @@ | |
| import sys | ||
|
|
||
| from collections import OrderedDict | ||
| from typing import Any, Mapping | ||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING, Any, Mapping | ||
|
|
||
| import pkg_resources | ||
|
|
||
| from .._settings import fragment_types as ft | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| # We only use Literal for type-checking and Mypy always brings its own | ||
| # typing_extensions so this is safe without further dependencies. | ||
| if sys.version_info < (3, 8): | ||
| from typing_extensions import Literal | ||
| else: | ||
| from typing import Literal | ||
|
|
||
| if sys.version_info < (3, 11): | ||
| import tomli as tomllib | ||
| else: | ||
| import tomllib | ||
|
|
||
|
|
||
| @dataclass | ||
| class Config: | ||
| package: str | ||
| package_dir: str | ||
| single_file: bool | ||
| filename: str | ||
| directory: str | None | ||
| version: str | None | ||
| name: str | None | ||
| sections: Mapping[str, str] | ||
| types: Mapping[str, Mapping[str, Any]] | ||
| template: str | ||
| start_string: str | ||
| title_format: str | Literal[False] | ||
| issue_format: str | None | ||
| underlines: list[str] | ||
| wrap: bool | ||
| all_bullets: bool | ||
| orphan_prefix: str | ||
|
|
||
|
|
||
| class ConfigError(Exception): | ||
| def __init__(self, *args: str, **kwargs: str): | ||
| self.failing_option = kwargs.get("failing_option") | ||
|
|
@@ -34,7 +64,7 @@ def __init__(self, *args: str, **kwargs: str): | |
|
|
||
| def load_config_from_options( | ||
| directory: str | None, config_path: str | None | ||
| ) -> tuple[str, Mapping[str, Any]]: | ||
| ) -> tuple[str, Config]: | ||
| if config_path is None: | ||
| if directory is None: | ||
| directory = os.getcwd() | ||
|
|
@@ -43,10 +73,10 @@ def load_config_from_options( | |
| config = load_config(base_directory) | ||
| else: | ||
| config_path = os.path.abspath(config_path) | ||
| if directory: | ||
| base_directory = os.path.abspath(directory) | ||
| else: | ||
| if directory is None: | ||
| base_directory = os.path.dirname(config_path) | ||
| else: | ||
| base_directory = os.path.abspath(directory) | ||
|
Comment on lines
+76
to
+79
Member
Author
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. I've flipped this logic to mirror the logic from above, because it was confusing to have the same check inverted. |
||
| config = load_config_from_file(os.path.dirname(config_path), config_path) | ||
|
|
||
| if config is None: | ||
|
|
@@ -55,7 +85,7 @@ def load_config_from_options( | |
| return base_directory, config | ||
|
|
||
|
|
||
| def load_config(directory: str) -> Mapping[str, Any] | None: | ||
| def load_config(directory: str) -> Config | None: | ||
|
|
||
| towncrier_toml = os.path.join(directory, "towncrier.toml") | ||
| pyproject_toml = os.path.join(directory, "pyproject.toml") | ||
|
|
@@ -70,14 +100,14 @@ def load_config(directory: str) -> Mapping[str, Any] | None: | |
| return load_config_from_file(directory, config_file) | ||
|
|
||
|
|
||
| def load_config_from_file(directory: str, config_file: str) -> Mapping[str, Any]: | ||
| def load_config_from_file(directory: str, config_file: str) -> Config: | ||
| with open(config_file, "rb") as conffile: | ||
| config = tomllib.load(conffile) | ||
|
|
||
| return parse_toml(directory, config) | ||
|
|
||
|
|
||
| def parse_toml(base_path: str, config: Mapping[str, Any]) -> Mapping[str, Any]: | ||
| def parse_toml(base_path: str, config: Mapping[str, Any]) -> Config: | ||
| if "tool" not in config: | ||
| raise ConfigError("No [tool.towncrier] section.", failing_option="all") | ||
|
|
||
|
|
@@ -134,22 +164,22 @@ def parse_toml(base_path: str, config: Mapping[str, Any]) -> Mapping[str, Any]: | |
| failing_option="template", | ||
| ) | ||
|
|
||
| return { | ||
| "package": config.get("package", ""), | ||
| "package_dir": config.get("package_dir", "."), | ||
| "single_file": single_file, | ||
| "filename": config.get("filename", "NEWS.rst"), | ||
| "directory": config.get("directory"), | ||
| "version": config.get("version"), | ||
| "name": config.get("name"), | ||
| "sections": sections, | ||
| "types": types, | ||
| "template": template, | ||
| "start_string": config.get("start_string", _start_string), | ||
| "title_format": config.get("title_format", _title_format), | ||
| "issue_format": config.get("issue_format"), | ||
| "underlines": config.get("underlines", _underlines), | ||
| "wrap": wrap, | ||
| "all_bullets": all_bullets, | ||
| "orphan_prefix": config.get("orphan_prefix", "+"), | ||
| } | ||
| return Config( | ||
| package=config.get("package", ""), | ||
| package_dir=config.get("package_dir", "."), | ||
| single_file=single_file, | ||
| filename=config.get("filename", "NEWS.rst"), | ||
| directory=config.get("directory"), | ||
| version=config.get("version"), | ||
| name=config.get("name"), | ||
| sections=sections, | ||
| types=types, | ||
| template=template, | ||
| start_string=config.get("start_string", _start_string), | ||
| title_format=config.get("title_format", _title_format), | ||
| issue_format=config.get("issue_format"), | ||
| underlines=config.get("underlines", _underlines), | ||
| wrap=wrap, | ||
| all_bullets=all_bullets, | ||
| orphan_prefix=config.get("orphan_prefix", "+"), | ||
| ) | ||
hynek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.