Skip to content
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

Fix some type errors #3440

Merged
merged 32 commits into from
May 17, 2024
Merged

Fix some type errors #3440

merged 32 commits into from
May 17, 2024

Conversation

jl-wynen
Copy link
Member

@jl-wynen jl-wynen commented May 6, 2024

This fixes some (a small fraction) type errors. Most of this is internal code. But making Mypy happy with it pointed out some problems in lower level annotations. So in particular the fixes in cpp_classes.pyi (by way of changes in C++ and the stubgen) should improve the situation for users.

There are many problems remaining. Even in the stub file itself. But those in particular need either a more sophisticated stubgen or an alternative solution. I didn't want to invest more time in it now.

Note that I refactored a few modules to help with type checks by making function interfaces clearer.

@jl-wynen
Copy link
Member Author

jl-wynen commented May 6, 2024

Note that I got lazy towards the end and started using python 3.10 features. So this PR will only work once we drop 3.9 in CI.

# Conflicts:
#	src/scipp/compat/wrapping.py
#	src/scipp/core/_sizes.py
#	src/scipp/core/assignments.py
#	src/scipp/core/concepts.py
#	src/scipp/core/variable.py
#	src/scipp/format/_parse.py
#	src/scipp/format/formatter.py
#	src/scipp/scipy/integrate/__init__.py
#	src/scipp/scipy/interpolate/__init__.py
#	src/scipp/scipy/ndimage/__init__.py
#	src/scipp/scipy/optimize/__init__.py
#	src/scipp/scipy/signal/__init__.py
#	src/scipp/units/__init__.py
#	tools/stubgen/__init__.py
#	tools/stubgen/transformer.py
src/scipp/_binding.py Outdated Show resolved Hide resolved


def _parse_dims_shape_sizes(
dims: list[str] | tuple[str, ...] | None = None,
shape: Sequence[int] | None = None,
sizes: dict[str, int] | None = None,
):
) -> Any:
Copy link
Member

Choose a reason for hiding this comment

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

Why is this one Any?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. I think it was just a quick and easy way to make Mypy shut up because this is an internal function. Changed it to be a bit more accurate.

src/scipp/_binding.py Outdated Show resolved Hide resolved
Comment on lines 10 to 11
if TYPE_CHECKING:
from .data_group import DataGroup
Copy link
Member

Choose a reason for hiding this comment

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

Why is it needed here? it's already imported in LN8

Copy link
Member Author

Choose a reason for hiding this comment

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

Dunno, removed it

@@ -305,7 +322,7 @@ def merge(lhs: Dataset, rhs: Dataset) -> Dataset: ...
def merge(lhs: DataGroup, rhs: DataGroup) -> DataGroup: ...


def merge(lhs, rhs):
def merge(lhs: Dataset | DataGroup, rhs: Dataset | DataGroup) -> Dataset | DataGroup:
Copy link
Member

Choose a reason for hiding this comment

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

Why not TypeVar here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Lazyness. Changed it.

Comment on lines +31 to +33
def __init__(self, keys: list[str], var: Variable) -> None:
self._var = var
self._keys = keys
Copy link
Member

Choose a reason for hiding this comment

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

Does the _keys need to have the reference to the original keys?

Suggested change
def __init__(self, keys: list[str], var: Variable) -> None:
self._var = var
self._keys = keys
def __init__(self, keys: Sequence[str], var: Variable) -> None:
self._var = var
self._keys = tuple(keys)

Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't. This was to avoid making a copy because that is not needed here. This class should never be instantiated by anyone outside of this module.

@jl-wynen jl-wynen merged commit 552d1a1 into main May 17, 2024
4 checks passed
@jl-wynen jl-wynen deleted the apease-mypy branch May 17, 2024 09:03
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.

None yet

2 participants