-
Notifications
You must be signed in to change notification settings - Fork 140
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
Ensure __version__ is retrieved while all dependencies are available #253
Comments
Fine on moving away from the deprecated method. I'm less keen on the idea of ignoring import errors: I'm trying to think about how getting the version number could be made properly robust, not just a bit less likely to fail. |
I think that would simply mean that we don’t try to get the version until we’ve installed all dependencies. |
Either that, or limiting how packages can specify their version number so we can always get it without loading the whole package. |
As long as |
I think I have seen AST browsing used to find an assignment to |
Sure, but that won’t work if there’s more complex code here. I want to retrieve the version from the latest git tag using |
I've mentioned before that I'd consider breaking |
Please give me a heads-up if you do that so I can move all of my packages to Poetry. |
Ah! that’s our difference, I’m in the camp of people who like having the canonical version in source code and have packaging metadata and VCS tags derived from it. |
I don’t want to start a discussion here, so let’s start a new issue to discuss this if you want to reply to this comment: My main reasons to not do that is because of 1. friction and 2. human fallibility: Your approach means to always perfectly execute: sed -Ei 's/"1\.3\.dev\d+"/"1\.4"' package/__init__.py
git add package/__init__.py
git commit -m 'v1.4'
sed -i 's/"1\.4"/"1\.4\.dev0"' package/__init__.py
git add package/__init__.py
git commit -m 'dev version'
git push
git push --tags That’s a lot of work and a lot can go wrong compared to my preferred git tag v1.4
git push --tags The minor reasons: 3. It pollutes your commit history with pairs of meaningless commits that 4. contain information that’s duplicated in your git tags (no one true source of information). 5. Theoretically you’d also need to bump I know there’s tools who do the above dance for you, but 6. I can never remember how their CLI works, and if you need to specify the new version or how to bump the version and if it pushes for you or not or … |
I have a feeling we've discussed this before, but briefly: inspecting a package's version number is something I absolutely don't want to do magic, to involve shelling out to git or trying to inspect the environment it's installed in. I want it to be stamped into the code of the package itself. Plus doing any clever things to get the version number usually means doing them at import time, which I also dislike I've also found that none of the available tools to automate changing version numbers quite fit my brain, so for now I live with updating version numbers by hand as part of a release. No, it's not ideal, but on a scale of things that concern me, it's not going to make the top 1000. I'm experimenting with making my own releaser tool that would fit my way of working ("modeltee"), but it's incomplete and I wouldn't recommend anyone try to use it yet. (To be clear, I'm still not saying I will make break your preferred way of doing this. If I see a way the different patterns can peacefully coexist, great. But if they conflict, I'll naturally prioritise the way I like to do things.) |
(Would be curious to read a few notes about your issues with bumpversion and others) |
OK! Getting back on topic: Is there anything that prevents us from delaying the |
Just got bit by this and found my way here. The fix was simple enough: just The linked issue flying-sheep/get_version#8 helped me see that there's another solution. I could move my If no proper fix is proposed, and if the current situation is the intended behaviour, I think it should be documented. E.g., something like this:
That long disclaimer takes some of the fun out of it, though. Even worse, I think (read: have not tested) that AST parsing would do the wrong thing in cases like this, as it would detect the string assignment and ignore the concatenation afterward: __version__ = '1.0.0'
__version__ += '-rc1' I'd like to pretend we live in a world where people don't do things like this, but 2020 has all but destroyed my optimism that people will act rationally. |
A hack I started to use in my projects is to only import things in the root package if we aren’t being imported by flit: from get_version import get_version
__author__ = "Philipp Angerer"
__version__ = get_version(__file__)
def within_flit():
import traceback
for frame in traceback.extract_stack():
if frame.name == "get_docstring_and_version_via_import":
return True
return False
if not within_flit():
from .submodule import thing
... |
@flying-sheep this hack only reaffirms my statement above about people acting rationally 😉 Here I agree with something @takluyver said in #257:
Another option, perhaps, is for Flit to have a setting (e.g., |
Once there’s a pure way to do this, I’ll immediately switch to it.
If my research is exhaustive enough to prove no tool does all 3, what I do is therefore the most rational option. PS: Even if poetry gains editable installs, the poetry plugin for VCS-sourced versions exists, but is hacky itself, as it relies on monkeypatching. |
@flying-sheep no offense intended; I guess the winking-face emoji wasn't enough to overcome the inherent coldness of text. To be more serious, your hack is indeed clever but I'm afraid it might be brittle as I don't think there's any guarantee that Regarding your criteria, if we assume that Flit satisfies the first two, then all that's left is a single source for the package version. I think this is already accomplished in simple cases where the published package metadata takes the version from |
Oh, it’s future-proof: if the function gets renamed or removed, the check will return I fully expect @takluyver to be responsible and only remove or rename
Yes: SCM version tags are necessary to be able to link commits to released versions, therefore they are the logical single source of truth.
Yes: If your package’s
from .version import __version__
from .core import business_logic
from setuptools_scm import get_version
__version__ = get_version(root='..', relative_to=__file__)
from third_party import ...
def business_logic(...): ... So what options exist?
|
There are ways of importing the module without going through the top-level package, such as the |
I think #382 is the more elegant way to do this. |
load_module
is deprecated. Andexec_module
has a distinct advantage:Since we try to get
__version__
from a module that possibly doesn’t have its dependencies installed, we could partially execute the module by ignoring exceptions (ImportError
s would be sufficient). If the module manages to already set its__version__
before anImportError
is raised,flit install
will still work.This allows to only add those dependencies to
build-requires
which are needed to get the__version__
, and letflit
do the rest.The text was updated successfully, but these errors were encountered: