Skip to content

🔧 Typing of ReqIFSpecHierarchy.children #193

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 4 commits into from
May 30, 2025

Conversation

ubmarco
Copy link
Contributor

@ubmarco ubmarco commented May 30, 2025

No description provided.

@ubmarco ubmarco force-pushed the mh-type-children branch from 944b102 to 96e3483 Compare May 30, 2025 07:07
@ubmarco ubmarco changed the title 🔧 Typing of ReqIFSpecHierarchy.childrech 🔧 Typing of ReqIFSpecHierarchy.children May 30, 2025
@stanislaw
Copy link
Contributor

stanislaw commented May 30, 2025

@ubmarco thanks for contributing. It looks like your test is failing due to an unrelated change.

I will both the unrelated change and some warnings that I am also seeing here.

After I ping you, could you fix your change and rebase-squash your commit to just one?

@stanislaw
Copy link
Contributor

Please disregard my original comment above and check how I updated it ☝️

@stanislaw
Copy link
Contributor

@ubmarco I have fixed the failing mypy check here: #194. Please rebase/squash your PR to just one commit and we are good to go.

@ubmarco ubmarco force-pushed the mh-type-children branch from f7fd94f to d2a374e Compare May 30, 2025 09:18
@ubmarco
Copy link
Contributor Author

ubmarco commented May 30, 2025

Rebased and squashed to 1 commit. I was unhappy with the nullable list, so I made the list mandatory.
Downstream tools do not have to check for None when adding children. Also helps with type checkers.

@stanislaw
Copy link
Contributor

Rebased and squashed to 1 commit. I was unhappy with the nullable list, so I made the list mandatory. Downstream tools do not have to check for None when adding children. Also helps with type checkers.

Thanks! Making the list mandatory could contradict to the approach that I was following so far where the nullability also mirrors to whether the corresponding XML elements will be generated or not. I believe you may now hit some failing test cases due to this because all spec hierarchies will now be generated with empty children which I think is not what we want for a general case.

@stanislaw
Copy link
Contributor

So I feel like your initial change with creating the [] in the add method would work very well.

@ubmarco
Copy link
Contributor Author

ubmarco commented May 30, 2025

Thanks for the insights. Reverted it.

Usage as a library with type hinting is still not optimal:

spec_hierarchy = ReqIFSpecHierarchy()
    identifier=create_uuid(),
    spec_object=spec_object.identifier,
    level=1,
)
if len(spec_hierarchy.children) > 2:
    pass

leads to

Argument of type "List[Unknown] | None" cannot be assigned to parameter "obj" of type "Sized" in function "len"
  Type "List[Unknown] | None" is not assignable to type "Sized"
    "None" is incompatible with protocol "Sized"
      "__len__" is not present

I would actually make it mandatory as instance attribute, then have an option how to unparse/serialize empty lists (leave out or create an empty list).

@stanislaw
Copy link
Contributor

stanislaw commented May 30, 2025

Thanks for the insights. Reverted it.

Usage as a library with type hinting is still not optimal:

spec_hierarchy = ReqIFSpecHierarchy()
    identifier=create_uuid(),
    spec_object=spec_object.identifier,
    level=1,
)
if len(spec_hierarchy.children) > 2:
    pass

leads to

Argument of type "List[Unknown] | None" cannot be assigned to parameter "obj" of type "Sized" in function "len"
  Type "List[Unknown] | None" is not assignable to type "Sized"
    "None" is incompatible with protocol "Sized"
      "__len__" is not present

I would actually make it mandatory as instance attribute, then have an option how to unparse/serialize empty lists (leave out or create an empty list).

In your example above, you would need to add an is not None check and the error would go away.

The idea behind this parse/unparse was that literally any ReqIF will be parsed/unparsed with the default settings. One big part of it is that some files have empty tags and some not. If you would push this detail down to the unparser level, you would then have to provide a special option when dealing with any arbitrary file which I think is not what we want. I wish I could do better with explaining this in the doc but that's pretty much the idea: the Python object model mirrors the XML tags:

If there is no tag, it is a None.
If there is a tag but no children, it is [].

@stanislaw
Copy link
Contributor

Having the above said, your PR as it is now makes a very good sense. I just noticed this unneeded from __future__ import annotations: please remove it and we will be good to go. I could do it myself but don't want to interfere with you pushing just in case.

@ubmarco
Copy link
Contributor Author

ubmarco commented May 30, 2025

This is a circular type reference, i.e. the type is not yet defined when using.
2 options:

  • as I did with the import
  • use quotes around the type, so it becomes a string (also ok for me)

PEP 563 offers the future import to get rid of the quotes for Python <3.14.

@stanislaw
Copy link
Contributor

Ah! I see now. Yes, it is ok to use quotes in your PR, and this is how I did it until now in reqif and the parent strictdoc. If we wanted to handle this project-wise, we could discuss it as a separate change. But I really don't mind having quotes since the code is all quote-based since long ago.

@ubmarco
Copy link
Contributor Author

ubmarco commented May 30, 2025

Done

@stanislaw stanislaw merged commit 0443a3d into strictdoc-project:main May 30, 2025
9 checks passed
@stanislaw
Copy link
Contributor

I could release a new Pip later today (the process is still semi-manual).

Were you planning to contribute any further PRs right away? Otherwise, I would just release this specific change.

@ubmarco
Copy link
Contributor Author

ubmarco commented May 31, 2025

Sure, release whenever it suits you. Thanks for the help getting this merged. Typing is quality of life :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants