-
Notifications
You must be signed in to change notification settings - Fork 250
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
Normalisation and Requirement's str()
#644
Comments
This is actually consistent in the face of https://peps.python.org/pep-0685/ requiring we normalize extras, but nothing requiring we normalize project names. We could potentially normalize project names as well, but that might break code that wants the original string. @pradyunsg opinions? |
The PEP requires the names to be normalized when comparing, it doesn't say they have to be changed to a normalized string by packaging, though, as far as I can tell. Maybe this:
This seems problematic in the short term, though, as previous versions don't consider these extra matching. I think it would be better to provide a transition period where extras can be written as specified, and comparison is normalized, before also normalizing the writing. Projects that historically have a |
The latest version of
Normalising the marker does appear to be required by the PEP. I think it'd be beneficial to review how requirements are represented and compared, e.g.:
|
I believe this change might be related to pypa/pip#11649 ? Specifically, the latest |
I think we should preserve them when we load them and represent them as-is; but compare them with normalisation. Basically, what @layday said. :) |
I'm pretty sure a PR would be welcome (and, unless @brettcannon feels differently, likely merged subject to regular code reviews). :) |
I was thinking about this today and I think there's a risk with the change, but one I think we an live with (i.e. I would be fine with a PR that changes the string representation to preserve the original string). Essentially it sucks when comparisons are non-obvious; the string you see will not be the string that comparisons are being made against. So we just need to be prepared to deal with that usability concern. Hopefully as PEP 685 gets adopted this will become less of an issue. |
We could also add a method that gives you a normalised form of the requirement. Unlike versions, this isn’t defined directly by the PEPs but we have clearly defined semantics for this? |
Does this mean to normalise the name, extras, versions in specifiers? Maybe additionally sort the extras and specifiers? |
Yes, pretty much. Get the requirement into a string that can be compared with the same semantics as a requirement equality. |
str()
Upon reading https://packaging.python.org/en/latest/specifications/core-metadata/#name:
However,
whereas
Is this intended? |
See pypa/packaging#644 (comment) Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
str()
str()
I think this issue is about trying to come with a good solution to this to rectify that kind of problem. |
I’d say preserving the original format for |
I'm personally happy with making equality handle the normalization and not care about the string representation. And if we construct a tuple for everything in the end then we can reuse that tuple for hashing. |
Fix pypagh-644. Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
Opened a pull request #696 let me know what you think. |
See pypa/packaging#644 (comment) Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
See pypa/packaging#644 (comment) Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
See pypa/packaging#644 (comment) Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
See pypa/packaging#644 (comment) Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
* Make `kedro micropkg package` accept `--verbose` Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Improve error when `micropkg pull` does not find sdist Fix gh-2542. Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Stop using pkg_resources Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Do not rely on setup.py to generate sdist See gh-2414. Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Stop relying on .egg-info directories Fix gh-2567. Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Note change from pkg_requirements See pypa/packaging#644 (comment) Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Improve code comments Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Fix equality checks of equivalent requirements Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Add micropackaging improvements to release notes Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Revert sdist check to make it more testable Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Fix micropkg pull error handling Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Add tests for new micropkg pull branches Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Remove untested path of private code Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Fix micropkg tests Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> * Add more detailed explanation of Requirement custom subclass Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com> --------- Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
Fix pypagh-644. Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
Fix pypagh-644. Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
IIUC #696 only canonicalises the name - shouldn't extras be canonicalised as well for comparison?
The originally reported issue hasn't been addressed either - extra markers are canonicalised when the requirement is stringified but names aren't (nor are their extras):
Can we reopen this? |
@layday reopened. |
Would it be possible to have at least a separated method that implements this normalisation (not necessarily That would facilitate a lot when writing For example nowadays, we can use $ pip-run 'packaging==24.1'
>>> from packaging.requirements import Requirement
>>> req = Requirement('mariadb>=1.0.1; python_version >= "3" and extra == "mariadb_connector"')
>>> str(req)
'mariadb>=1.0.1; python_version >= "3" and extra == "mariadb_connector"' (Optionally, this hypothetical method could also decide to normalise the requirement name, but that is not mandated by any PEP yet I believe). |
Originally posted by @layday in pypa/build#550 (comment)
Build's test suite is broken because packaging 22 is normalizing the extras when producing strings. But it doesn't do this with dependency names. It seems like it should be consistent.
The text was updated successfully, but these errors were encountered: