-
Notifications
You must be signed in to change notification settings - Fork 15
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
Fix some type errors #3440
Conversation
This is needed to correctly match types in functions that take unions as arguments and pass those to functions annotated with VariableLikeType.
The previous suppression ignored the fact that some packages modules are implemented in their __init__ files, that is, the __init__ files are not simply collections of imports.
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/core/_sizes.py
Outdated
|
||
|
||
def _parse_dims_shape_sizes( | ||
dims: list[str] | tuple[str, ...] | None = None, | ||
shape: Sequence[int] | None = None, | ||
sizes: dict[str, int] | None = None, | ||
): | ||
) -> 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 is this one 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.
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/core/_cpp_wrapper_util.py
Outdated
if TYPE_CHECKING: | ||
from .data_group import DataGroup |
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 is it needed here? it's already imported in LN8
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.
Dunno, removed it
src/scipp/core/operations.py
Outdated
@@ -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: |
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 not TypeVar here?
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.
Lazyness. Changed it.
def __init__(self, keys: list[str], var: Variable) -> None: | ||
self._var = var | ||
self._keys = keys |
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.
Does the _keys
need to have the reference to the original keys
?
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) |
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 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.
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.