-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Add meaningful tests to new-layout #2366
Add meaningful tests to new-layout #2366
Conversation
cfe6865
to
2e4b81f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Full disclosure: I was the one to suggest upstreaming this after @fabianhauser proposed this change in a poetry-generated project.]
Diff LGTM, change is sensible. 👍
Ship it!
Thank you for taking the time to contribute to the poetry. 👍 The This change adds an otherwise unused dev dependency |
Yeah, we're aware of that. Actually, that's the reason for this PR, as it's thus the respective newly generated project's developers' responsibility to keep the versions in In contrast, the example test currently generated with the template introduces more duplication without any meaningful benefit. If the example test was the test suggested by this PR, some projects might choose to keep and adopt it while adding their own tests, while other projects might choose to delete it. In contrast, with the example test currently generated with the template, the only reasonable action after An example test that's worth keeping as an actual test if the generated projects' developers so choose is IMO a good thing.
That is indeed the downside of this PR. If this PR was to be reconsidered, would you prefer the entry in |
Hello, I would suggest a complete different approach: The version of the package shouldn't be hardcoded within the code. Instead Option 1: Completely removal of
And add this to the
The most clean version would be, to check if the project should support python <3.8 at all (or vice versa). If not the additional stuff for this should not be included. fin swimmer |
What about the case of a microservice using poetry (not an actual python package)? |
Hello @aviramha,
IMO in that case you should: a) distribute your fin swimmer |
If this were the case, would it still be possible to read the version from within python (either from within the package, or when imported by another package)?
I feel like there's got to be a better solution than adding additional dependencies Anyway, if this is the standard moving forward, will this be added to |
That'd be only an additional dependency for Python < 3.8. For Python ≥ 3.8, |
Currently, python/mypy#1153 blocks this from being a great solution, as it currently produces:
Since there is no timeline on a fix, as mentioned in a comment in the linked issue thread, maybe a solution from bhrutledge/mypy-importlib-metadata could be useful. |
* Add version.py * Use method to get version from python-poetry/poetry#2366 (comment) * Add importlib-metadata dependency for python versions less than 3.8
I'm testing this out using semantic versioning. I do the following: poetry version 0.1.0-alpha.0
# [package-name] 0.1.0-alpha.0 is returned When I then specify that in the version test, I get the following:
Seems somehow
|
This happens when the package is build, because poetry allows you to follow SemVer specifications in the |
The use of |
Could you give an example of where this may not work? Does If so, I believe that the Here's a script that creates an example project layout with the package folder in a
#!/bin/sh
set -eux
mkdir -p temp/src/temp/
mkdir -p temp/tests/
cd temp
cat << EOF > pyproject.toml
[tool.poetry]
name = "temp"
version = "0.1.0"
description = ""
authors = []
packages = [
{ include = "temp", from = "src" },
]
[tool.poetry.dependencies]
python = "^3.7"
importlib-metadata = {version = "latest", python = "<3.8"}
[tool.poetry.dev-dependencies]
pytest = "^5.2"
[tool.poetry.scripts]
temp = "temp.main:main"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
EOF
cat << EOF > src/temp/__init__.py
"""
This file causes python to treat the folder it's in as a package.
It's also used to find the version string.
"""
import sys
if sys.version_info >= (3, 8):
from importlib.metadata import version as metadata_version
else:
from importlib_metadata import version as metadata_version
__version__ = str(metadata_version(__name__))
EOF
cat << EOF > src/temp/__main__.py
"""
This is the file run when this project is run as a module:
python -m module_name
"""
from .main import main
if __name__ == "__main__":
main()
EOF
cat << EOF > src/temp/main.py
"""
The main file for when the project is run
"""
from argparse import ArgumentParser
from . import __version__
def main() -> None:
parser = ArgumentParser()
parser.add_argument("--version", action="version", version=__version__)
parser.parse_args()
if __name__ == "__main__":
main()
EOF
touch tests/__init__.py
cat << EOF > tests/test_version.py
"""
tests for the version string only
"""
import re
from temp import __version__
simple_semver_re = re.compile(r"^(\\d+\\.){2}\\d+$")
def test_version() -> None:
"make sure the version is a string that matches semver format"
assert isinstance(
__version__, str
), f"__version__ must be a str, not '{type(__version__)}'"
assert simple_semver_re.match(
__version__
), f"'{__version__}' is not in simple semver.org format"
EOF
poetry install
poetry run temp --version
poetry run python -m temp --version
poetry run pytest |
@mawillcockson I agree that
[tool.poetry]
name = "oh-util-is-taken-in-pypi-so-i-have-to-change-the-distname"
packages = [
{ include = "util" },
] (Though this may be not good from the viewpoint of avoiding possible conflicts.) |
That's a very good point. Finding the distribution name programmatically is somewhat involved, though it is possible. If the project is being generated through If anyone reading this runs into this, in order to use the example I proposed, the distribution name of the project must match the package name: the name of the module in which the
The name of the command that the package includes can still be anything:
|
See this for more details: python-poetry/poetry#2366 (comment)
…d to exactly one. See also python-poetry/poetry#2366 (comment) for recommended practice
I love Poetry for everything. 🚀 But I've faced similar issues like those described here. So, I just made a plugin (available with Poetry # __init__.py
__version__ = "0.1.0" or from a git tag, set with a GitHub release or with: $ git tag 0.1.0 I thought this could be useful for others here too as this seems to be the place of the main conversation. https://github.com/tiangolo/poetry-version-plugin |
What about using
-> https://setuptools.readthedocs.io/en/latest/pkg_resources.html#distribution-attributes |
FYI, we (@dialoguemd) do this ^. We use It is updated automatically at CI whenever something is merged on main branch by using python semantic-release. |
Does this also work for packages installed in development mode ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
I tried adding this code manually to my project, but after bumping the version the test no longer passed. To be sure, I tried with a fresh example:
The only work around I found was to manually remove the poetry virtual env. |
Poetry no longer adds any test or any dependencies when creating a new project via Closing as this is no longer relevant. |
Thank you @fabianhauser for getting this together, we appreciate the contribution. |
For now the version and iformation will be updated using a git hook and nox target. When the project is upgraded to 3.8 we should replace this mechanism by the use of importlib.metadata. see also: * #145 (comment) * python-poetry/poetry#2366 (comment) * #135 (comment)
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This PR adds a more meaningful template-test to the
poetry new
standard layout. The new test checks whether the module-version and thepyproject.toml
-version are equal.Pull Request Check List
Resolves: -