Skip to content

chore: cleanup of get_dtype (with numpy 2.5 support) - #285

Open
vincentkoppen wants to merge 13 commits into
mainfrom
pgm/support-numpy-2-5-0-with-cleanup
Open

chore: cleanup of get_dtype (with numpy 2.5 support)#285
vincentkoppen wants to merge 13 commits into
mainfrom
pgm/support-numpy-2-5-0-with-cleanup

Conversation

@vincentkoppen

Copy link
Copy Markdown
Member

A small cleanup of the get_dtype function:

  • Directly determine the errors based on annotations instead of the dtypes dict
  • Remove the dtypes dict, directly create the dtype_list
    • The string length is now a list comprehension. We could also improve the logic, so that "appending" will check it or something. But this is a first simple cleanup

Should make Sonarcloud happy and make the flow a bit simpler to understand.

Signed-off-by: Vincent Koppen <vincent.koppen@alliander.com>
Signed-off-by: Vincent Koppen <vincent.koppen@alliander.com>
Signed-off-by: Vincent Koppen <vincent.koppen@alliander.com>
Signed-off-by: Vincent Koppen <vincent.koppen@alliander.com>
Signed-off-by: Vincent Koppen <vincent.koppen@alliander.com>
Signed-off-by: Vincent Koppen <vincent.koppen@alliander.com>
Signed-off-by: Vincent Koppen <vincent.koppen@alliander.com>
@vincentkoppen
vincentkoppen marked this pull request as ready for review June 26, 2026 11:45
Base automatically changed from pgm/support-numpy-2-5-0 to main July 7, 2026 07:44
jaapschoutenalliander and others added 6 commits July 13, 2026 13:01
Signed-off-by: jaapschoutenalliander <jaap.schouten@alliander.com>
Signed-off-by: Vincent Koppen <53343926+vincentkoppen@users.noreply.github.com>
Signed-off-by: jaapschoutenalliander <jaap.schouten@alliander.com>
Signed-off-by: jaapschoutenalliander <jaap.schouten@alliander.com>
Signed-off-by: jaapschoutenalliander <jaap.schouten@alliander.com>
Signed-off-by: jaapschoutenalliander <jaap.schouten@alliander.com>
@sonarqubecloud

Copy link
Copy Markdown

@vincentkoppen vincentkoppen left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks good to me overall.
Just some nitpicks, nothing major. Take a look if you agree with any of them, otherwise this is good to.
(can't approve, since it's my PR)

# Expected type_args for NDArray3: (NDArray[numpy.float64], typing.Literal[3])
if len(type_args) == 2: # noqa: PLR2004
dtype = get_args(type_args[0])[0]
return (name, _resolve_str_dtype(name, dtype, str_lengths), get_args(type_args[1])[0])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

nitpick: personally I would prefer to apply _resolve_str_dtype after the parsing.
So: parse all dtypes -> apply the str filter over all -> return

The str parsing isn't specific to the parsing per numpy version. Now this has to be applied 4 times and is possiby easily forgotten if a new parsing is added.

raise ValueError(f"dtype {type_def} not understood or supported")


def _parse_annotation_post_25(name: str, type_def: Any, type_args: tuple, str_lengths: dict[str, int]) -> tuple:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

nitpick: sonarcloud isn't happy with the return type. I think we could do something like tuple[str, np.generic | str,...] or more explicit: tuple[str, np.generic | str] | tuple[str, np.generic | str, int]

The | str is only needed if we keep the string conversion. Also it might need type[np.generic] instead of np.generic, not sure.

return dtype


def _parse_annotation_pre_25(name: str, type_def: Any, type_args: tuple, str_lengths: dict[str, int]) -> tuple:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same for this fopr the return type.

Self = TypeVar("Self", bound="FancyArray")


def _resolve_str_dtype(name: str, dtype: Any, str_lengths: dict[str, int]) -> Any:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Typing can be improved here as well I think, if np.generic or type[np.generic] is correct in the previous comments, then it can also be used here.

Comment on lines +162 to +165
dtype_list = [
parse_annotation(name, type_def, get_args(type_def), str_lengths) for name, type_def in annotations.items()
]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

  • nitpick: Prefer to use keyword args
  • The fact that we have both type_def and type_args feels a strange. But I think this is needed mainly for the tests right?
    Otherwise it's hard to test both python versions with only passing type_def?
    Fine to keep it as is, but I would also be fine with:
dtype_list = []
for name, dtype in annotations.items(): 
  try: 
    dtype_args = get_args(dtype)
    dtype_list.append(parse_annotations(name, type_args, ...))
exception: 
   raise ValueError(".... {type_def}....")

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