Type-annotate ansible.galaxy.collection - #85961
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
a59e959 to
dd991a4
Compare
dd991a4 to
d55b934
Compare
| @dataclass | ||
| class ManifestControl: | ||
| directives: list[str] = None | ||
| directives: list[str] |
There was a problem hiding this comment.
This seems like a user-facing change, shouldn't it remain optional?
| directives: list[str] | |
| directives: list[str] | None = None |
There was a problem hiding this comment.
This class is only used to pass things between calls internally, though. But I get your point. MyPy wanted me to do something where it's initialized, I think. I'll double-check if I can revert this bit.
| def __init__(self, reasons=None, stdout=None, rc=None, ignore=False): | ||
| def __init__( | ||
| self, | ||
| reasons: t.Iterable | None = None, |
There was a problem hiding this comment.
| reasons: t.Iterable | None = None, | |
| reasons: t.Iterable[str] | None = None, |
| t.cast(list[str], collection_meta.get('build_ignore', [])), | ||
| t.cast( | ||
| ManifestMetadataType | t.Type[Sentinel], | ||
| collection_meta.get('manifest', Sentinel), | ||
| ), |
There was a problem hiding this comment.
_normalize_galaxy_yml_manifest populates the default value for the optional keys (so it contains both 'manifest' and 'build_ignore'). Can we fix the type hint to match, instead of using collection_meta.get()?
There was a problem hiding this comment.
I can try. MyPy was mad at me when I was trying to fix it w/o a cast (it thought that the _build_files_manifest() call gets an object which I couldn't explain). I'd prefer that but couldn't make it work for hours. I'd be happy to get rid of the casts if we find a way.
| if manifest_control is None: | ||
| manifest_control = {} | ||
|
|
||
| try: | ||
| control = ManifestControl(**manifest_control) | ||
| control = ManifestControl(**(manifest_control or {'directives': []})) |
There was a problem hiding this comment.
This works if manifest_control is None or empty, but if it's truthy and doesn't contain 'directives', it will fail since ManifestControl now requires directives.
There was a problem hiding this comment.
@s-hertel it's required in the typing: https://github.com/ansible/ansible/pull/85961/files#diff-4b4657f16ad17de4ea445f2ecec926b0aa800a6065283dcb07eb88491cb94d83R34. Wouldn't that be enough?
There was a problem hiding this comment.
It's a change in behavior. I get a crash now when I using a manifest containing the option other than directives.
| collection_manifest['file_manifest_file']['chksum_sha256'] = secure_hash_s(files_manifest_json, hash_func=sha256) | ||
| collection_manifest_json = to_bytes(json.dumps(collection_manifest, indent=True), errors='surrogate_or_strict') | ||
| collection_manifest_with_hash = { | ||
| **collection_manifest, | ||
| 'file_manifest_file': { | ||
| **collection_manifest['file_manifest_file'], | ||
| 'chksum_sha256': secure_hash_s( | ||
| files_manifest_json, | ||
| hash_func=sha256, | ||
| ), | ||
| }, | ||
| } | ||
| collection_manifest_json = to_bytes( | ||
| json.dumps(collection_manifest_with_hash, indent=True), | ||
| errors='surrogate_or_strict', | ||
| ) |
There was a problem hiding this comment.
I think the original is easier to read, especially the first line versus the replacement.
There was a problem hiding this comment.
@s-hertel yes, but we can't mutate read-only fields. I was trying to make the data structs immutable. Perhaps, we need structs with and w/o the check sum computed. Dunno how to best handle this.
| def _get_direct_collection_namespace( | ||
| self, | ||
| collection: Collection, | ||
| ) -> str | None: |
There was a problem hiding this comment.
| ) -> str | None: | |
| ) -> str: |
| def _get_direct_collection_name( | ||
| self, | ||
| collection: Collection, | ||
| ) -> str | None: |
There was a problem hiding this comment.
| ) -> str | None: | |
| ) -> str: |
There was a problem hiding this comment.
get_direct_collection_meta() sets name+namespace to None in a few cases and so this can just represent that. Otherwise, MyPy will be unhappy about non-matching typing declarations.
There was a problem hiding this comment.
If these return None, the only caller will crash with a TypeError.
| # type: (t.Union[Candidate, Requirement]) -> dict[str, str] | ||
| def get_direct_collection_dependencies( | ||
| self, | ||
| collection: Candidate | Requirement, |
There was a problem hiding this comment.
Can this also use Collection, for consistency?
| collection: Candidate | Requirement, | |
| collection: Collection, |
There was a problem hiding this comment.
Maybe, I think in some places I just got the comments converted but in some places it was problematic. I'll check locally, thanks!
| sentinel_keys = set() # type: set[str] | ||
| galaxy_yml: dict[ | ||
| str, | ||
| str | list[str] | dict[str, str] | None | t.Type[Sentinel], |
There was a problem hiding this comment.
The manifest boolean option omit_default_directives is not represented here.
| str | list[str] | dict[str, str] | None | t.Type[Sentinel], | |
| str | list[str] | dict[str, str | bool] | None | t.Type[Sentinel], |
There was a problem hiding this comment.
Yeah, I temporarily gave up on this function as it's non-trivial to address (hence the cast at the end). It should definitely be refactored more in the future. But I didn't want to make the PR scope too big. I'll accept this suggestion but it'll only cover a little bit of what needs to be improved.
| ): # type: (...) -> dict | ||
| b_path: bytes, | ||
| filename: str, | ||
| ) -> dict: |
There was a problem hiding this comment.
We don't actually validate the MANIFEST.json is a dictionary before we use it as such.
There was a problem hiding this comment.
True. But I also just converted the type from comment to explicit syntax. So I didn't look into it. I was trying not to get into making runtime changes unless absolutely necessary. Do you feel like the PR should make such changes?
There was a problem hiding this comment.
I just noticed the bug while reviewing, you don't need to fix it.
| SIGNATURE_COUNT_RE = r"^(?P<strict>\+)?(?:(?P<count>\d+)|(?P<all>all))$" | ||
|
|
||
|
|
||
| class DisplayThread: |
There was a problem hiding this comment.
Note: I should try to move this back into the original place now that I've solved the display type with a protocol. Originally, I was going to use this class in the union.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This patch is a result of `pyrefly autotype` with *a lot* of post-processing.
d55b934 to
00eabe3
Compare
|
|
||
| if t.TYPE_CHECKING: | ||
| from ansible.utils.display import Display | ||
| from ._types import DisplayQueueType, DisplayThreadProto |
There was a problem hiding this comment.
(no idea why I have DisplayQueueType here still; will probably have to drop it before merge)
| from ._types import DisplayQueueType, DisplayThreadProto | |
| from ._types import DisplayThreadProto |
|
(last push is just a rebase on top of the moved-out portions of the initial patch; no new changes) |
| DisplayQueueType: _t.TypeAlias = _q.Queue[DisplayQueueItemType] | ||
|
|
||
|
|
||
| class DisplayThreadProto(_t.Protocol): |
There was a problem hiding this comment.
does this make sense to have here or should it go to a more general _types file?
There was a problem hiding this comment.
It may make sense elsewhere. But most of this patch is trying to figure out bits and pieces within the scope of a subpackage/module. I'd definitely consider moving it in a follow-up, but I wouldn't want to include anything beyond ansible.galaxy.collection in the PR scope — it's too fragile atm.
mattclay
left a comment
There was a problem hiding this comment.
There's a lot going on in this PR. Could you split it up to make it easier to review?
There are quite a few "simple" conversions from type comments to native type annotations. That seems like a good place to start, as those should be easy to review. That would make it easier to review the other, more involved changes (which may also benefit from being split up).
|
@mattclay agreed. I've actually done that with 3 PRs before PTO and was thinking about doing it again, just haven't returned to this one yet. It's on my list. |
This patch is a result of
pyrefly autotypewith a lot of post-processing.ISSUE TYPE