Skip to content
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

BUG: improve validation of pyproject.toml meson-python configuration #304

Merged
merged 3 commits into from
Mar 7, 2023

Conversation

dnicolodi
Copy link
Member

Switch from an incomplete (an bugged) ad hoc validation to a scheme based validation strategy. The scheme is defined in the function _validate_pyproject_config() as nested dictionaries where the keys are configuration field names and valued are validation functions. Unknown fields result in an error.

Fixes #293.

@dnicolodi dnicolodi force-pushed the pyproject-toml branch 10 times, most recently from fe7904b to e0363cc Compare February 9, 2023 23:08
@FFY00 FFY00 added the enhancement New feature or request label Mar 1, 2023
@dnicolodi dnicolodi force-pushed the pyproject-toml branch 3 times, most recently from e1962a5 to 1947798 Compare March 1, 2023 11:30
@dnicolodi dnicolodi added this to the v0.13.0 milestone Mar 1, 2023
@dnicolodi
Copy link
Member Author

We need this (or something similar) to fix #293, ideally before the next release.

@dnicolodi dnicolodi added the bug Something isn't working label Mar 1, 2023
mesonpy/__init__.py Show resolved Hide resolved
mesonpy/__init__.py Show resolved Hide resolved
Copy link
Member

@FFY00 FFY00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor comments, then I think it is ready for merging. Though, as mentioned in the other PR, we will probably move this code to pyproject-metadata when it implements the unknown keys check functionality anyway.

tests/test_pep517.py Outdated Show resolved Hide resolved
tests/test_pep517.py Outdated Show resolved Hide resolved
assert isinstance(name, str)
return name.replace('-', '_')
"""Project name."""
return str(self._metadata.name).replace('-', '_')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str is not needed here. The assert was because mypy was complaining because of the import IIRC.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That field can only ever be a string.

https://pep621.readthedocs.io/en/latest/#pyproject_metadata.StandardMetadata.name

I don't think mypy is loading the type hint correctly, hence the assert.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the version of mypy used in the CI, later versions of mypy do not complain

mesonpy/__init__.py Show resolved Hide resolved

def _string(value: Any, name: str) -> str:
if not isinstance(value, str):
raise ConfigError(f'only one value for "{name}" can be specified')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error messages assumes it can only be a container, while in fact it can be anything. For most PEP 517 frontends, it will be a container of some sort, but that is not guaranteed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it can be only a string or a list of strings. It is written in the PEP

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PEP only makes a recommendation when frontends are building the config settings from a user provided string mapping 😅

In the PEP, you can see 3 words capitalized, "MUST", "SHOULD", and "MAY", they indicate obligation, recommendation, and optionality, respectively. So while the PEP does recommend for that to be the case, it does not mandate it. It also explicitly mentions that fronts can have an alternative mechanism to build the dictionary.

Build frontends MAY also provide arbitrary other mechanisms for users to place entries in this dictionary.

It's a simple enough check anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, if you need to quote from the PEP, do no quote only the part that makes it look like it supports your claims:

This argument, which is passed to all hooks, is an arbitrary dictionary provided as an “escape hatch” for users to pass ad-hoc configuration into individual package builds. Build backends MAY assign any semantics they like to this dictionary. Build frontends SHOULD provide some mechanism for users to specify arbitrary string-key/string-value pairs to be placed in this dictionary. For example, they might support some syntax like --package-config CC=gcc. In case a user provides duplicate string-keys, build frontends SHOULD combine the corresponding string-values into a list of strings. Build frontends MAY also provide arbitrary other mechanisms for users to place entries in this dictionary.

The dictionary is string-key, string-value. You can use arbitrary mechanism to add entries to the dictionary, but the dictionary has string keys and string values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not quote the part we were talking about because you were already referring to it, so I assumed you knew where it was. I only quoted the arbitrary mechanism bit to make it easy for you to ctrl+f it and see exactly what I was referring to.

The dictionary is string-key, string-value.

It doesn't say that anywhere, the first sentence even says it is an arbitrary dictionary.

In fact, I have considered adding support for loading config_settings from a Python file in pypa/build.

You can use arbitrary mechanism to add entries to the dictionary

Yes.

but the dictionary has string keys and string values.

No, the user options provided via that mechanism are.

My takeaways for that paragraph are:

  • config_setting is an arbitrary dictionary
  • It is intended for backends to assign semantics to it if they want
  • The PEP recommends that frontends to implement a mechanism to allow users to add settings to the dictionary
    • If a user provide duplicated keys via this mechanism, they should be combined into a list

Anyway, I don't think this is being productive. Feel free to merge this PR whenever you think it's good enough to merge.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please take your time and read all the text?

Build frontends SHOULD provide some mechanism for users to specify arbitrary string-key/string-value pairs to be placed in this dictionary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dnicolodi dnicolodi force-pushed the pyproject-toml branch 4 times, most recently from 91f3766 to 025e022 Compare March 7, 2023 22:23
Switch from an incomplete (an bugged) ad hoc validation to a scheme
based validation strategy. The scheme is defined in the function
_validate_pyproject_config() as nested dictionaries where the keys are
configuration field names and valued are validation functions. Unknown
fields result in an error.

Fixes mesonbuild#293.
Code can be simplified using pyproject_metadata.StandardMetadata also
when we infer the package metadata from meson.build.
PEP 517 specifies that the options are received as a dictionary with
string keys and string values, thus there is no need to verify that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Meson-python crashes when specifying an incomplete [tool.meson-python.args] section in pyproj.toml
2 participants