Skip to content

Add Typing to baseio, baserawio, and exampleio - #1334

Merged
apdavison merged 9 commits into
NeuralEnsemble:masterfrom
zm711:typing
Jan 26, 2024
Merged

apdavison merged 9 commits into
NeuralEnsemble:masterfrom
zm711:typing

Conversation

@zm711

@zm711 zm711 commented Oct 13, 2023

Copy link
Copy Markdown
Contributor

Replaces #1229 because that now has merge conflicts.

I know this has been discussed #1123. Sam is against, others are typically for. This is just a draft to think about (& correct or reject), but I think adding type hints to some places exposed to the user would be beneficial. By putting the hints into the base it propagates to all ios to minimize other typing needing to be added, although developers can override the typing in their specific io.

I know that neo has complicated inputs, but I think type hints work best to give a suggested appropriate input (And as a nod to Sam I didn't add type hints for the returns yet).

@pep8speaks

pep8speaks commented Oct 13, 2023 •

Copy link
Copy Markdown

Hello @zm711! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2023-12-09 15:32:30 UTC

@zm711

zm711 commented Oct 13, 2023

Copy link
Copy Markdown
Contributor Author

I'll fix formatting after I know whether this is supported or not.

@JoeZiminski

Copy link
Copy Markdown

I would add my support to such a PR. Typing is fantastic for catching bugs, in general improving robustness. Since I started using type hints last year they have caught a lot of potential bugs and saved me a lot of trouble!

@samuelgarcia

Copy link
Copy Markdown
Contributor

Having the role of the old dinosaur is not so easy in a group but I am trying my best to maintain my public character.

@zm711

zm711 commented Oct 16, 2023 •

Copy link
Copy Markdown
Contributor Author

Having the role of the old dinosaur is not so easy in a group but I am trying my best to maintain my public character.

@samuelgarcia never change. Your example error messages here and in probeinterface are always the highlight of my proofreading. (to be clear I mean in a great way :) )

@zm711
zm711 marked this pull request as ready for review November 26, 2023 18:19
@alejoe91

Copy link
Copy Markdown
Contributor

@zm711 this looks good to me!

@samuelgarcia can we merge?

@zm711

zm711 commented Nov 28, 2023

Copy link
Copy Markdown
Contributor Author

Thanks @alejoe91. I have a feeling when there is typing in the PR title @samuelgarcia has a filter for his alerts to jeter dans la poubelle.

@samuelgarcia

Copy link
Copy Markdown
Contributor

@zm711 it is even worst. typing PR are triggering in me a brain state nearby freezing.
I always need to turn off the computer for at least one hour to recover totally.

@apdavison apdavison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am in favour of this PR, but some changes are needed for the type hints to be compatible with Python 3.8 and 3.9.

Comment thread neo/io/baseio.py Resolved
Comment thread neo/io/baseio.py Resolved
Comment thread neo/rawio/baserawio.py Resolved
Comment thread neo/rawio/baserawio.py Resolved
Comment thread neo/rawio/baserawio.py Resolved
Comment thread neo/rawio/baserawio.py Resolved
Comment thread neo/rawio/baserawio.py Resolved
Comment thread neo/io/baseio.py Resolved
Comment thread neo/rawio/baserawio.py Resolved
@zm711

zm711 commented Dec 9, 2023 •

Copy link
Copy Markdown
Contributor Author

I am in favour of this PR, but some changes are needed for the type hints to be compatible with Python 3.8 and 3.9.

I think I addressed all of this in comments. But just to overall explain for future reference

I have added:

from __future__ import annotations

to the three files with typing, which allows us to use type hints from 3.9 and 3.10 in 3.8, 3.9, and 3.10 to keep things clean. This means we can:

  • use | instead of needing Literal, but if Literal is preferred I can switch to that.
  • use built-in types even in 3.8 list instead of List, dict instead of Dict

EDIT: one tiny other note is that the testing suite is currently running pytest on 3.9 (for NeoIOTest) so if | was going to break the code we should've seen that during the testing.

@apdavison

Copy link
Copy Markdown
Member

so if | was going to break the code we should've seen that during the testing.

yes, that confused me, but since you've explained about from __future__ import annotations it all makes sense!

There's one remaining point I'm not sure about (numpy.dtype), but otherwise this looks good.

@zm711

zm711 commented Dec 9, 2023 •

Copy link
Copy Markdown
Contributor Author

Officially the better typing would be:

import numpy.typing as npt

my_function(x, dtype: npt.DTypeLike = 'int32')

This indicates that something can be coerced into a numpy dtype. I currently have the dtype listed as the dtype constructor which is the constructor that takes an input and attempts to convert it to a numpy.dtype. So @apdavison you are correct that the current type is not strictly correct. Would you like me to change or do you think people will understand despite the mention of the constructor rather than the typing. I think the issue we will run into is that some old versions of numpy won't have the numpy typing module so it would be harder to support.

https://numpy.org/devdocs/reference/typing.html

@apdavison

Copy link
Copy Markdown
Member

I think the issue we will run into is that some old versions of numpy won't have the numpy typing module so it would be harder to support

That's a good point. For now we're not doing strict type checking with mypy or whatever, so I agree let's leave it as is for now.

@zm711

zm711 commented Dec 9, 2023 •

Copy link
Copy Markdown
Contributor Author

Also just for reference I checked and typing was added in 1.20 (but I think the DtypeLike was added in 1.21) so we would need to wait until our minimum numpy is that to be safe.

@alejoe91 alejoe91 added this to the 0.13.0 milestone Jan 24, 2024
@apdavison
apdavison merged commit 32c3b99 into NeuralEnsemble:master Jan 26, 2024
@zm711
zm711 deleted the typing branch April 6, 2024 20:49
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.

6 participants