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

Feature/python typing/components command.py #2223

Merged

Conversation

NovakApis
Copy link
Contributor

PR checklist

  • This comment contains a description of changes (with reason)
    Added types to the components_command.py
    As this is the main component used in other components, let us nail this one!

Copy link
Contributor

@awgymer awgymer left a comment

Choose a reason for hiding this comment

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

I wonder if we need to think quite carefully about the approach to type hinting within tools?

File-by-file seems like a good approach but would we benefit from some custom types (e.g. a pathlike = Union[str, Path] being defined somewhere central that can be used across the codebase rather than redefining those sorts of unions in each file?

nf_core/components/components_command.py Outdated Show resolved Hide resolved
Copy link
Member

@mirpedrol mirpedrol left a comment

Choose a reason for hiding this comment

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

looks good :)

@mirpedrol mirpedrol self-requested a review March 28, 2023 15:27
@mirpedrol
Copy link
Member

After reading @awgymer comments, I think it makes sense to talk about it a bit more 🙂

@fabianegli
Copy link
Contributor

Hi Novak

I just pushed a commit to this PR to add mypy checks to the CI. This should help to make sure we don't add any conflicting type annotations. I am not sure if it is entirely correct, but we'll see the test once the CI runs.

👋

@codecov
Copy link

codecov bot commented Mar 28, 2023

Codecov Report

Merging #2223 (5c88dbf) into dev (a220bcd) will increase coverage by 0.14%.
Report is 10 commits behind head on dev.
The diff coverage is 64.86%.

❗ Current head 5c88dbf differs from pull request most recent head 9171fa0. Consider uploading reports for the commit 9171fa0 to get more accurate results

@@            Coverage Diff             @@
##              dev    #2223      +/-   ##
==========================================
+ Coverage   70.60%   70.74%   +0.14%     
==========================================
  Files          87       87              
  Lines        9437     9439       +2     
==========================================
+ Hits         6663     6678      +15     
+ Misses       2774     2761      -13     
Files Coverage Δ
nf_core/components/components_command.py 71.32% <94.11%> (ø)
nf_core/components/components_utils.py 46.51% <40.00%> (-1.69%) ⬇️

... and 2 files with indirect coverage changes

Changed components_utils->get_repo_info to return a tuple of three concrete types instead of a list of three Any types.
Implemented suggestion to include self.dir as None. This was done in the __init__ argument typing
Copy link
Member

@mirpedrol mirpedrol left a comment

Choose a reason for hiding this comment

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

Second try with a more conscious review, taking into account all the comments we have been discussing :)
My line of thought was to only annotate variables which are class arguments or declared such empty lists, etc. Feel free to disagree! 🙂

nf_core/components/components_command.py Outdated Show resolved Hide resolved
nf_core/components/components_command.py Outdated Show resolved Hide resolved
nf_core/components/components_command.py Outdated Show resolved Hide resolved
nf_core/components/components_command.py Outdated Show resolved Hide resolved
nf_core/components/components_command.py Outdated Show resolved Hide resolved
nf_core/components/components_command.py Outdated Show resolved Hide resolved
nf_core/components/components_command.py Outdated Show resolved Hide resolved
nf_core/components/components_command.py Outdated Show resolved Hide resolved
nf_core/components/components_command.py Outdated Show resolved Hide resolved
@awgymer
Copy link
Contributor

awgymer commented Mar 29, 2023

I have never used mypy properly before but my assumption is that when it runs it will flag anything that is not annotated or derivable that should be, so that the best way to proceed will be to add fewer annotations and only add things like the above examples if mypy complains?

If I am wrong about how mypy works then feel free to disregard

@NovakApis
Copy link
Contributor Author

NovakApis commented Mar 29, 2023

As it stands, mypy will fail every time we run tests. Doing a thorough check breaks on third-party library imports (first lines of .py files). The current repo doesn't include stubs for:

  • yaml - stub
  • questionary
  • rich
  • git
  • prompt_toolkit
  • packaging
  • requests stub
  • requests_cache

Is it wise to create a new issue including stubs for these libraries?
What if some are not provided with a stub, should we skip them in mypy tests?

@fabianegli
Copy link
Contributor

The mypy config in the pyproject.toml should solve the missing stub issue. (in theory at least. hopefully...)

fixed syntax error for ingroe_missing_imports and added follow_imports="skip"
@fabianegli
Copy link
Contributor

@mashehu suggested we limit the checking to a PR-by-PR basis

The problem with that approach is that now it will add files to the checks even if someone just changes a docstring somewhere and then that is blocked unless the whole file is correctly type hinted because of previous - PR unrelated unrelated - false type annotations. It will create a greater mess than simply removing erroneous type hints now.

@mashehu Please reconsider

@kedhammar
Copy link
Contributor

@fabianegli one problem I am having with eliminating repository-wide MyPy errors is that the typing of external packages can contradict itself, e.g.

nf_core/utils.py:56: error: Argument 1 to "join" has incompatible type "Optional[str]"; expected "Union[str, PathLike[str]]"

here the output types of os.environ.get() is incompatible with the expected inputs of os.path.join().

Do you have any good ideas in mind for addressing this?

@kedhammar
Copy link
Contributor

Summary:

  • I've decreased the scope of MyPy to only check modified files.
  • I've addressed MyPy errors by mostly trimming out superfluous typing and in some cases by minor refactoring of the code to prevent issues.

I tried expanding the scope of MyPy to the entire repository and addressed all issues I could find by running MyPy locally (see c97ef6e). However, even if the repo passed locally the GHA CI threw a lot of new errors which I believe would be too time consuming to address during the Hackathon.

@fabianegli you are welcome to create a new PR to expand the scope of MyPy to the entire repo, but @mashehu asked me to proceed with the limited scope.

.github/workflows/lint-code.yml Outdated Show resolved Hide resolved
.github/workflows/lint-code.yml Outdated Show resolved Hide resolved
nf_core/components/components_command.py Outdated Show resolved Hide resolved
nf_core/components/components_utils.py Show resolved Hide resolved
nf_core/components/components_utils.py Outdated Show resolved Hide resolved
kedhammar and others added 5 commits October 17, 2023 15:58
"because we can"

Co-authored-by: Matthias Hörtenhuber <mashehu@users.noreply.github.com>
Co-authored-by: Matthias Hörtenhuber <mashehu@users.noreply.github.com>
no context for this arg ever being undefined

Co-authored-by: Matthias Hörtenhuber <mashehu@users.noreply.github.com>
Co-authored-by: Matthias Hörtenhuber <mashehu@users.noreply.github.com>
Change typing based on nf_core.utils.determine_base_dir() outputs. A bit messy, but let's not increase the scope further.

Co-authored-by: Matthias Hörtenhuber <mashehu@users.noreply.github.com>
@kedhammar kedhammar dismissed stale reviews from mirpedrol and fabianegli October 17, 2023 14:06

outdated

@kedhammar kedhammar removed the request for review from maxulysse October 17, 2023 14:22
@kedhammar kedhammar merged commit 873da41 into nf-core:dev Oct 17, 2023
16 of 18 checks passed
@fabianegli
Copy link
Contributor

nf_core/utils.py:56: error: Argument 1 to "join" has incompatible type "Optional[str]"; expected "Union[str, PathLike[str]]"

here the output types of os.environ.get() is incompatible with the expected inputs of os.path.join().

Do you have any good ideas in mind for addressing this?

This is exactly why type hints are useful. They tell us that what we coded together might not wrk as expected. It basically tells us that we need to handle the case when the environment variable is not found and os.environ.get() returns None, because None can not be joined. So to fix this, one should ask the question "What should tools do, when said environment variable is undefined?" and then adapt the code accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Stale PRs for typing Add type annotations to nf-core/tools
6 participants