Skip to content

Mypy check untyped functions#5673

Merged
wyli merged 17 commits intoProject-MONAI:devfrom
f-schnabel:mypy_check_untyped_functions
Dec 12, 2022
Merged

Mypy check untyped functions#5673
wyli merged 17 commits intoProject-MONAI:devfrom
f-schnabel:mypy_check_untyped_functions

Conversation

@f-schnabel
Copy link
Copy Markdown
Contributor

Fixes #5657.

Description

Enable config to also type check untyped functions (not in tests).

  • Type annotations were added
  • Explicit casts were added
  • Variable redefinitions with other types were refactored
  • If no other option was available (or it would be overly verbose) # type: ignore was added

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

@f-schnabel
Copy link
Copy Markdown
Contributor Author

f-schnabel commented Dec 7, 2022

I'm a bit confused by the auto fix by pre-commit.ci. I thought that type unions with A | B syntax was only added in python 3.10 but MONAI is targeting versions >= 3.7.
See also here: https://docs.python.org/3/library/typing.html#typing.Union

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Dec 7, 2022

I'm a bit confused by the auto fix by pre-commit.ci. I thought that type unions with A | B syntax was only added in python 3.10 but MONAI is targeting versions >= 3.7. See also here: https://docs.python.org/3/library/typing.html#typing.Union

that particular file has from __future__ import annotations which triggers pyupgrade of pre-commit.ci to update the type annotations

Signed-off-by: Felix Schnabel <[email protected]>
@f-schnabel
Copy link
Copy Markdown
Contributor Author

Is it ok to add venv to the exclude path of black or should it be in another PR with an extra issue?
It was just quite annoying since my whole pc froze as black tried to format all dependencies, but I can remove it from this PR.

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Dec 7, 2022

Is it ok to add venv to the exclude path of black or should it be in another PR with an extra issue? It was just quite annoying since my whole pc froze as black tried to format all dependencies, but I can remove it from this PR.

sure. could you please also avoid assert https://deepsource.io/gh/Project-MONAI/MONAI/run/66546629-66ba-4424-8758-2fe9f51ac2ed/python/BAN-B101?listindex=0 you can use isinstance to check the types

@f-schnabel f-schnabel force-pushed the mypy_check_untyped_functions branch from 2003556 to df06306 Compare December 10, 2022 14:12
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Dec 11, 2022

/build

@f-schnabel f-schnabel marked this pull request as ready for review December 11, 2022 14:59
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Dec 12, 2022

/build

@wyli wyli merged commit c489de0 into Project-MONAI:dev Dec 12, 2022
@wyli
Copy link
Copy Markdown
Contributor

wyli commented Dec 12, 2022

thanks @Shadow-Devil, I can see that the type annotation enhancements addressed a few potential bugs, that's great.

@f-schnabel
Copy link
Copy Markdown
Contributor Author

@wyli Thank you for apprechiating my work! I would like to go on and as my next type checking enhancement type the deprecated & deprecated_args decorators, since otherwise all functions annotated with them lose type information. Do you think this is a good next step? If yes, I will create an issue and work on it😄

@wyli
Copy link
Copy Markdown
Contributor

wyli commented Dec 12, 2022

thanks, I think those utilities will still need some refactoring (such as for #5653), type checking support could come later after that. But it's a good idea and please help create a feature request...

@f-schnabel f-schnabel deleted the mypy_check_untyped_functions branch December 12, 2022 12:57
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.

Add mypy typchecking of untyped functions for source code (not tests)

2 participants