Skip to content

Conversation

@rijobro
Copy link
Contributor

@rijobro rijobro commented Jan 6, 2021

Description

Avoid import *'s. Add to runtests.sh for future additions.

Status

Ready

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Integration tests passed locally by running ./runtests.sh --codeformat --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick.
  • Documentation updated, tested make html command in the docs/ folder.

@wyli
Copy link
Contributor

wyli commented Jan 6, 2021

this is a good point, is it possible to achieve the same by not ignoring F405 flake8 and modifyiing some config?

E203,E305,E402,E501,E721,E741,F403,F405,F821,F841,F999,W503,W504,C408,E302,W291,E303,

@rijobro
Copy link
Contributor Author

rijobro commented Jan 6, 2021

Yup, looks good. What do you think I should I do for import * in __init__.py files?

@wyli
Copy link
Contributor

wyli commented Jan 6, 2021

Yup, looks good. What do you think I should I do for import * in __init__.py files?

this tool suggests to set __all__ https://lgtm.com/projects/g/Project-MONAI/MONAI/alerts/?mode=tree&ruleFocus=3980091 what do you think?

@rijobro
Copy link
Contributor Author

rijobro commented Jan 6, 2021

import * is fine when __all__ is defined, but I think readability is reduced since you don't necessarily know where something was imported from. Personally, I would prefer explicitly importing individual modules.

But I'm not sure if import * is OK inside of an __init__.py file...

@wyli
Copy link
Contributor

wyli commented Jan 6, 2021

import * is fine when __all__ is defined, but I think readability is reduced since you don't necessarily know where something was imported from. Personally, I would prefer explicitly importing individual modules.

But I'm not sure if import * is OK inside of an __init__.py file...

sure I'm fine with either. I think both are valid for __init__.py. @ericspod @Nic-Ma comments?
(https://lgtm.com/rules/3980091/)

@ericspod
Copy link
Member

ericspod commented Jan 7, 2021

Wildcard imports in __init__.py files is standard if __all__ is defined in the module being imported. Wildcard imports elsewhere are discouraged by flake8 and other sources. Somehow you want to export from a module the things you want to be part of the public interface, so filtering is done in the __init__.py file with selective imports from submodules or with wildcards and __all__ used in submodules to define what's public. From what I've seen the selective imports isn't as common, it has the effect of defining what should be public in a different place from where things are actually defined. Using __all__ keeps everything about a definition in one file.

What MONAI does is a bit of a mix at the moment, what I had implemented before was an export decorator to fill the role of __all__ but I think now we're better off without it, though still using the auto-import load_submodules routine. What we don't want is the statement from monai import * to import every definition into the current namespace, there needs to be some hierarchy somewhere still.

Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
@rijobro
Copy link
Contributor Author

rijobro commented Jan 7, 2021

Couldn't figure out how to set flake8 up so that import * was allowed inside of __init__.py provided __all__ was defined in the import module. In the end I simply removed all import *.

@rijobro rijobro requested review from ericspod and wyli January 7, 2021 11:32
@wyli
Copy link
Contributor

wyli commented Jan 7, 2021

thanks, looks good to me.

optionally could add a sentence in CONTRIBUTING.md about adding new function/class?

(also doesn't setting per-file-ignores work? https://flake8.pycqa.org/en/latest/user/options.html#cmdoption-flake8-per-file-ignores

per-file-ignores = __init__.py: F401
)

@rijobro
Copy link
Contributor Author

rijobro commented Jan 7, 2021

optionally could add a sentence in CONTRIBUTING.md about adding new function/class?

Will do.

(also doesn't setting per-file-ignores work? https://flake8.pycqa.org/en/latest/user/options.html#cmdoption-flake8-per-file-ignores

We could ignore all __init__.py files, but that's not really what we want (we want to flag up an error if import * is used with no __all__).

@wyli
Copy link
Contributor

wyli commented Jan 7, 2021

/integration-test

Signed-off-by: Richard Brown <33289025+rijobro@users.noreply.github.com>
Copy link
Contributor

@wyli wyli left a comment

Choose a reason for hiding this comment

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

thanks it looks good to me.

@rijobro rijobro merged commit 1f74ca2 into Project-MONAI:master Jan 7, 2021
@rijobro rijobro deleted the wildcard_import branch January 7, 2021 17:43
@rijobro rijobro mentioned this pull request Jan 12, 2021
3 tasks
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