Skip to content

Commit

Permalink
fix: require version in dynamic if unset
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Nov 3, 2023
1 parent 188c54a commit cff7e55
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
9 changes: 5 additions & 4 deletions pyproject_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ class StandardMetadata:

def __post_init__(self) -> None:
self.name = re.sub(r'[-_.]+', '-', self.name).lower()
self._update_dynamic(self.version)

@classmethod
def from_pyproject(
Expand Down Expand Up @@ -226,10 +225,14 @@ def from_pyproject(

version_string = fetcher.get_str('project.version')
requires_python_string = fetcher.get_str('project.requires-python')
version = packaging.version.Version(version_string) if version_string else None

if version is None and 'version' not in dynamic:
raise ConfigurationError('field "project.version" missing and "version" not specified in "project.dynamic"')

Check warning on line 231 in pyproject_metadata/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyproject_metadata/__init__.py#L231

Added line #L231 was not covered by tests

return cls(
name,
packaging.version.Version(version_string) if version_string else None,
version,
fetcher.get_str('project.description'),
cls._get_license(fetcher, project_dir),
cls._get_readme(fetcher, project_dir),
Expand All @@ -250,8 +253,6 @@ def from_pyproject(
def _update_dynamic(self, value: Any) -> None:
if value and 'version' in self.dynamic:
self.dynamic.remove('version')
elif not value and 'version' not in self.dynamic:
self.dynamic.append('version')

def __setattr__(self, name: str, value: Any) -> None:
# update dynamic when version is set
Expand Down
47 changes: 45 additions & 2 deletions tests/test_standard_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
textwrap.dedent('''
[project]
name = true
version = '0.1.0'
'''),
('Field "project.name" has an invalid type, expecting a string (got "True")'),
),
Expand All @@ -37,6 +38,7 @@
textwrap.dedent('''
[project]
name = true
version = '0.1.0'
dynamic = [
'name',
]
Expand All @@ -57,6 +59,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
license = true
'''),
('Field "project.license" has an invalid type, expecting a dictionary of strings (got "True")'),
Expand All @@ -65,6 +68,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
license = {}
'''),
('Invalid "project.license" value, expecting either "file" or "text" (got "{}")'),
Expand All @@ -73,6 +77,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
license = { file = '...', text = '...' }
'''),
(
Expand All @@ -84,6 +89,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
license = { made-up = ':(' }
'''),
(
Expand All @@ -94,6 +100,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
license = { file = true }
'''),
('Field "project.license.file" has an invalid type, expecting a string (got "True")'),
Expand All @@ -102,6 +109,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
license = { text = true }
'''),
('Field "project.license.text" has an invalid type, expecting a string (got "True")'),
Expand All @@ -110,6 +118,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
license = { file = 'this-file-does-not-exist' }
'''),
('License file not found ("this-file-does-not-exist")'),
Expand All @@ -119,6 +128,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
readme = true
'''),
(
Expand All @@ -130,6 +140,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
readme = {}
'''),
(
Expand All @@ -140,6 +151,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
readme = { file = '...', text = '...' }
'''),
(
Expand All @@ -151,6 +163,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
readme = { made-up = ':(' }
'''),
(
Expand All @@ -161,6 +174,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
readme = { file = true }
'''),
('Field "project.readme.file" has an invalid type, expecting a string (got "True")'),
Expand All @@ -169,6 +183,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
readme = { text = true }
'''),
('Field "project.readme.text" has an invalid type, expecting a string (got "True")'),
Expand All @@ -177,6 +192,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
readme = { file = 'this-file-does-not-exist', content-type = '...' }
'''),
('Readme file not found ("this-file-does-not-exist")'),
Expand All @@ -185,6 +201,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
readme = { file = 'README.md' }
'''),
('Field "project.readme.content-type" missing'),
Expand All @@ -193,6 +210,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
readme = { text = '...' }
'''),
('Field "project.readme.content-type" missing'),
Expand All @@ -202,6 +220,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
description = true
'''),
('Field "project.description" has an invalid type, expecting a string (got "True")'),
Expand All @@ -211,6 +230,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
dependencies = 'some string!'
'''),
('Field "project.dependencies" has an invalid type, expecting a list of strings (got "some string!")'),
Expand All @@ -219,6 +239,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
dependencies = [
99,
]
Expand All @@ -229,6 +250,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
dependencies = [
'definitely not a valid PEP 508 requirement!',
]
Expand All @@ -243,6 +265,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
optional-dependencies = true
'''),
(
Expand All @@ -254,6 +277,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
[project.optional-dependencies]
test = 'some string!'
'''),
Expand All @@ -266,6 +290,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
[project.optional-dependencies]
test = [
true,
Expand All @@ -280,6 +305,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
[project.optional-dependencies]
test = [
'definitely not a valid PEP 508 requirement!',
Expand All @@ -295,6 +321,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
requires-python = true
'''),
('Field "project.requires-python" has an invalid type, expecting a string (got "True")'),
Expand All @@ -304,6 +331,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
keywords = 'some string!'
'''),
('Field "project.keywords" has an invalid type, expecting a list of strings (got "some string!")'),
Expand All @@ -312,6 +340,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
keywords = [
true,
]
Expand All @@ -323,6 +352,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
authors = {}
'''),
(
Expand All @@ -334,6 +364,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
authors = [
true,
]
Expand All @@ -348,6 +379,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
maintainers = {}
'''),
(
Expand All @@ -359,6 +391,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
maintainers = [
10
]
Expand All @@ -373,6 +406,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
classifiers = 'some string!'
'''),
('Field "project.classifiers" has an invalid type, expecting a list of strings (got "some string!")'),
Expand All @@ -381,6 +415,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
classifiers = [
true,
]
Expand All @@ -392,6 +427,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
[project.urls]
homepage = true
'''),
Expand All @@ -402,6 +438,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
[project.urls]
documentation = true
'''),
Expand All @@ -412,6 +449,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
[project.urls]
repository = true
'''),
Expand All @@ -422,6 +460,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
[project.urls]
changelog = true
'''),
Expand All @@ -432,6 +471,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
scripts = []
'''),
('Field "project.scripts" has an invalid type, expecting a dictionary of strings (got "[]")'),
Expand All @@ -441,6 +481,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
gui-scripts = []
'''),
('Field "project.gui-scripts" has an invalid type, expecting a dictionary of strings (got "[]")'),
Expand All @@ -450,6 +491,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
entry-points = []
'''),
(
Expand All @@ -461,6 +503,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
entry-points = { section = 'something' }
'''),
(
Expand All @@ -472,6 +515,7 @@
textwrap.dedent('''
[project]
name = 'test'
version = '0.1.0'
[project.entry-points.section]
entrypoint = []
'''),
Expand Down Expand Up @@ -684,6 +728,7 @@ def test_requires_python(value):
pyproject_metadata.StandardMetadata.from_pyproject({
'project': {
'name': 'example',
'version': '0.1.0',
'requires-python': value,
},
})
Expand All @@ -700,5 +745,3 @@ def test_version_dynamic():
})
metadata.version = packaging.version.Version('1.2.3')
assert 'version' not in metadata.dynamic
metadata.version = None
assert 'version' in metadata.dynamic

0 comments on commit cff7e55

Please sign in to comment.