Repository navigation
Small validator refactorings #809
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f938ba6
Move `Validation` protocol and impls to `validate` module
danieldk 3ff9268
Rename `Validator` to `MetadataValidator`
danieldk 2c9894a
Rename `validator` argument to `metadata_validator`
danieldk 29b8cfc
Make `MetadataValidator`s take `Metadata` and the variant `str`
danieldk f450e38
Fixup call that was not updated
danieldk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Protocol | ||
|
|
||
| from kernels_data import Metadata, Version | ||
| from packaging.version import InvalidVersion, parse | ||
|
|
||
| from kernels.archs import _check_arch_incompatibility | ||
| from kernels.backends import _backend | ||
| from kernels.python_deps import validate_dependencies | ||
|
|
||
|
|
||
| class MetadataValidator(Protocol): | ||
| """Metadata validator for a kernel build variant.""" | ||
|
|
||
| def validate_metadata(self, *, metadata: Metadata, variant: str) -> None: ... | ||
|
|
||
|
|
||
| class DependencyValidator: | ||
| """Validate the Python dependencies of a kernel build variant.""" | ||
|
|
||
| def validate_metadata(self, *, metadata: Metadata, variant: str) -> None: | ||
| validate_dependencies( | ||
| metadata.name.python_name, | ||
| metadata.python_depends, | ||
| _backend(), | ||
| ) | ||
|
|
||
|
|
||
| class ArchValidator: | ||
| """Validate architecture compatibility for a kernel build variant.""" | ||
|
|
||
| def validate_metadata(self, *, metadata: Metadata, variant: str) -> None: | ||
| _check_arch_incompatibility(metadata, variant) | ||
|
|
||
|
|
||
| def _installed_version() -> Version | None: | ||
| """The installed `kernels` version as a numeric version. | ||
|
|
||
| Pre-release and development suffixes are stripped, since kernel metadata | ||
| only records release versions. Without stripping, a development version | ||
| like `0.17.0.dev0` would compare as older than the `0.17.0` it implements. | ||
|
|
||
| Returns `None` if the installed version is not a PEP 440 version, which | ||
| can happen for versions derived from a VCS checkout. Such a version cannot | ||
| be compared, and this check must never make a kernel fail to load. | ||
| """ | ||
| # Avoid an import cycle. | ||
| from kernels import __version__ | ||
|
|
||
| try: | ||
| release = parse(__version__).release | ||
| except InvalidVersion: | ||
| return None | ||
|
|
||
| # packaging < 22 returns a LegacyVersion with `release = None` for | ||
| # non-PEP 440 versions instead of raising `InvalidVersion`. | ||
| if release is None: | ||
| return None | ||
|
|
||
| return Version.from_str(".".join(str(part) for part in release)) | ||
|
|
||
|
|
||
| class MinverValidator: | ||
| """Validate that the installed `kernels` library meets the minimum version | ||
| required by a kernel.""" | ||
|
|
||
| def validate_metadata(self, *, metadata: Metadata, variant: str) -> None: | ||
| minver = metadata.kernels_minver | ||
| if minver is None: | ||
| return | ||
|
|
||
| installed = _installed_version() | ||
| if installed is not None and installed < minver: | ||
| # Report the verbatim installed version, not the normalized one used | ||
| # for comparison, so the message matches what `pip show` reports. | ||
| # Avoid an import cycle. | ||
| from kernels import __version__ | ||
|
|
||
| raise RuntimeError( | ||
| f"Kernel '{metadata.name}' variant '{variant}' requires " | ||
| f"kernels>={minver}, but version {__version__} is installed. " | ||
| "Upgrade with: pip install --upgrade kernels" | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class AllValidator: | ||
| """Apply multiple validators to a kernel dependency tree.""" | ||
|
|
||
| validators: list[MetadataValidator] | ||
|
|
||
| def validate_metadata(self, *, metadata: Metadata, variant: str) -> None: | ||
| for validator in self.validators: | ||
| validator.validate_metadata(metadata=metadata, variant=variant) | ||
|
|
||
|
|
||
| def default_metadata_validators() -> list[MetadataValidator]: | ||
| """The metadata validators that are applied to every kernel dependency tree.""" | ||
| return [DependencyValidator(), MinverValidator()] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this should be a generic validator still because we might want to add a kernel validator in the future and we will have to add another argument for that, right?
Uh oh!
There was an error while loading. Please reload this page.
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.
For kernel validators there would be a
kernel_validator: KernelValidatorargument. Otherwise we'd haveMetadataValidator | KernelValidator, but you would not be able to pass both.Uh oh!
There was an error while loading. Please reload this page.
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.
Or, we'd need another
Validatorprotocol, but it couldn't define any methods, since the arguments would be different between metadata and kernel validators.