-
Notifications
You must be signed in to change notification settings - Fork 37
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
chore!: restructure plugin models #775
chore!: restructure plugin models #775
Conversation
27af4e5
to
8e88178
Compare
This class was vestigial, being a child of PluginPropertiesModel, while all plugins' properties classes are children of PluginProperties, which is itself a child of PluginPropertiesModel. This is a breaking change because external plugins for applications (e.g. snapcraft) inherit from both PluginProperties and PluginModel.
8e88178
to
edd76ff
Compare
ff77dd9
to
fcc9096
Compare
This does several things: - Merges PluginProperties and PluginPropertiesModel - Makes PluginProperties.unmarshal() work for most plugins be default - Makes PluginProperties.marshal() fully dump json-able objects - Adds a `plugin` field to all plugins, providing their names.
fcc9096
to
2d2e216
Compare
bded288
to
1899868
Compare
0ba2c27
to
3d9308d
Compare
craft_parts/plugins/properties.py
Outdated
validate_assignment=True, | ||
) | ||
|
||
plugin: Any = "" |
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.
why Any?
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.
Is it to play nice with the various Literal["x"]
overrides in subclasses?
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.
It was left over from what @tigarmo suggests when I was playing around with different ways to make creating child nicer and cleaner with pyright. In the end I went with double-declaring frozen=True
though, so I'll switch this back to str
craft_parts/plugins/properties.py
Outdated
@@ -65,4 +81,7 @@ def get_pull_properties(cls) -> list[str]: | |||
def get_build_properties(cls) -> list[str]: | |||
"""Obtain the list of properties affecting the build stage.""" | |||
properties = cls.schema(by_alias=True).get("properties") | |||
return list(properties.keys()) if properties else [] | |||
if properties: | |||
del properties["plugin"] |
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.
how safe is this?
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 guess it should be safe since if the code reaches this then the plugin
entry is there, but it could be written as properties.pop("plugin", None)
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.
It should be safe (the plugin
key not existing in the properties dict would mean we have a much bigger problem), but I rewrite it with a list comprehension anyway since that's faster and safer
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.
(And if you meant because of the del
, yes that's safe too, as schema
creates a new dict each time.)
craft_parts/plugins/properties.py
Outdated
@@ -65,4 +81,7 @@ def get_pull_properties(cls) -> list[str]: | |||
def get_build_properties(cls) -> list[str]: | |||
"""Obtain the list of properties affecting the build stage.""" | |||
properties = cls.schema(by_alias=True).get("properties") | |||
return list(properties.keys()) if properties else [] | |||
if properties: | |||
del properties["plugin"] |
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 guess it should be safe since if the code reaches this then the plugin
entry is there, but it could be written as properties.pop("plugin", None)
|
||
from overrides import override | ||
|
||
from .base import Plugin | ||
from .properties import PluginProperties | ||
|
||
|
||
class NilPluginProperties(PluginProperties): | ||
class NilPluginProperties(PluginProperties, frozen=True): |
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.
shouldn't this one have _required_fields = ("plugin",)
?
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.
Somewhat counter-intuitively, no. The name _required_fields
came from it being the required
argument in extract_plugin_properties
, but in reality it's more like _extra_fields
or _fields_that_dont_start_with_the_plugins_name
.
Looking at this though, extract_plugin_properties
is only actually used here, so I'm thinking about dumping it altogether and moving the logic here anyway.
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.
Ok, I've moved the logic from extract_plugin_properties
into PluginProperties.unmarshal()
and used the model's properties to determine the fields to use.
craft_parts/plugins/properties.py
Outdated
validate_assignment=True, | ||
) | ||
|
||
plugin: Any = "" |
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.
Is it to play nice with the various Literal["x"]
overrides in subclasses?
I missed some changes when I was playing with PluginProperties. The plugin name will always be a string.
Moves the logic from extract_plugin_properties directly into PluginProperties.unmarshal()
b168041
to
95e5fa6
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.
Nice refactor!
This does several related things:
PluginModel
fromcraft_parts.plugins.base
PluginPropertiesModel
andPluginProperties
(which didn't need separation anymore)unmarshal
method ofPluginProperties
usable by all plugins (and deletes the unnecessary overrides)