8000 Some typing improvements by SimonHeybrock · Pull Request #19 · scipp/scippnexus · GitHub
[go: up one dir, main page]

Skip to content

Some typing improvements #19

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 7 commits into from
Apr 13, 2022
Merged

Some typing improvements #19

merged 7 commits into from
Apr 13, 2022

Conversation

SimonHeybrock
Copy link
Member

There are a lot of remaining mypy errors. Not sure what to do with most of them, for now.

@SimonHeybrock SimonHeybrock requested a review from jl-wynen April 11, 2022 12:41
@@ -159,7 +159,7 @@ def _getitem(self, select: ScippIndex) -> sc.DataArray:
continue
try:
sel = to_child_select(self.dims, field.dims, select)
coord = self[name][sel]
coord: sc.Variable = self[name][sel]
Copy link
Member
@nvaytet nvaytet Apr 12, 2022

Choose a reason for hiding this comment

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

Not sure I see why this is required here (and other instances below). In what cases do we need to do this?
Do we also need it everywhere in scipp/neutron/ess?

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem (and the reason for many of the remaining errors that I did not manage (or care) to fix yet) is that __getitem__ may returns different things. Simplest example: h5py.Group.__getitem__ may give you a h5py.Group or a h5py.Dataset.

Do you know a solution?

Copy link
Member

Choose a reason for hiding this comment

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

Is it limited to Group and Dataset or can it be something else?
If it's only those two, can you define what the __getitem__ will return at its definition? e.g.

def __getitem__(self, ...) -> Union[Group, Dataset]:

I don't even know if that would help?..

Copy link
Member Author
@SimonHeybrock SimonHeybrock Apr 12, 2022

Choose a reason for hiding this comment

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

On the contrary, that is the source of the problem. If I now do anything with the return value I get complaints that the method may not exist, or follow-up errors about the type of the next __getitem__.

Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that every time we get an item from a python dict somewhere, we need to specify the type of what we are expecting?

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, that is what we have to find out. Someone must have had this problem before us.

Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that every time we get an item from a python dict somewhere, we need to specify the type of what we are expecting?

Depends on how the dict is defined. If it is e.g. Dict[str, Union[int, str, tuple]], then yes, we would have to check every usage. And strictly speaking that makes sense because we don't know what we are getting. Usually we rely on Python's duck typing and getting TypeError or similar exceptions when the type does not match our expectations.

But typically, we would have a more concrete type in the dict: Dict[str, str] or any: Dict[str, Any] or just dict. In those cases, type checking is disabled.

Copy link
Member

Choose a reason for hiding this comment

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

Is type checking something we want to strictly enforce everywhere in our codebase, or is it starting to get in the way of our development?

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 can catch bugs, so I feel we should try to use it more. Whether that means strictly enforcing it is something I cannot answer for now.


Ellipsis = ellipsis.Ellipsis
Copy link
Member

Choose a reason for hiding this comment

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

Did you mean lowercase ellipsis? Right now, it conflicts the else branch.

Copy link
Member Author

Choose a reason for hiding this comment

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

In what way? We need ellipsis to be a type (the class defined here). I got this from python/typing#684 (comment).

Copy link
Member

Choose a reason for hiding this comment

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

Looks to me like Ellipsis is only defined in order to support if a is Ellipsis: in the example. In our case, writing 'Ellipsis' in code outside of typing.py would refer to the builtin type instead. So I am not sure the type checker would be happy with it.
In any case, I think line 66 can be removed in our case.

@SimonHeybrock SimonHeybrock merged commit 8bc1f9a into main Apr 13, 2022
@SimonHeybrock SimonHeybrock deleted the some-typing-improvements branch April 13, 2022 09:17
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.

3 participants
0