-
Notifications
You must be signed in to change notification settings - Fork 56
Coding style
Even though relative imports are prevalent in ort, they are confusing, harder to manage and refactor. We should prefer absolute imports for clarity and robustness.
- https://softwareengineering.stackexchange.com/questions/159503/whats-wrong-with-relative-imports-in-python
- https://google.github.io/styleguide/pyguide.html#22-imports
Import only modules (in most cases inside modules that is not __init__.py
).
Import only the module instead of classes and functions to (1) keep the namespace clean and provide readers with more context on where its members come from, and (2) prevent circular import errors.
- Prevent circular import errors: Programming FAQ — Python 3.10.4 documentation
Circular imports are fine where both modules use the "import" form of import. They fail when the 2nd module wants to grab a name out of the first ("from module import name") and the import is at the top level. That's because names in the 1st are not yet available, because the first module is busy importing the 2nd.
- Clean namespace: For example, readers don't need to backtrack to see sleep is a function from time, as opposed to a function defined in the file. https://google.github.io/styleguide/pyguide.html#22-imports
Allow imports at the module toplevel only, unless (1) it is too expensive to load the module or (2) module may not be available.
- It is clear what a module needs when all imports are grouped in a single place. This makes refactoring easy.
- Otherwise, any import errors will be raised only when the code executes. Erroneous imports may go undetected until the code path is hit at runtime.
- https://pylint.readthedocs.io/en/latest/user_guide/messages/convention/import-outside-toplevel.html
onnxscript/tests
contains integration tests and common test utilities (common/
). Module unit tests should be created next to the module source with the name <module name>_test.py
. This makes them easy to discover and obvious when unit tests are missing.