ert icon indicating copy to clipboard operation
ert copied to clipboard

Avoid import outside top-level

Open berland opened this issue 1 year ago • 2 comments

Keeping as exception at one spot due to circular dependency and one spot due to measurable time-loss to do top-level import.

Ignoring the issue in script/build, this file is being deleted soon.

Issue Resolves ruff PLC0415

Approach fix/add exception/comment.

  • [ ] PR title captures the intent of the changes, and is fitting for release notes.
  • [x] Added appropriate release note label
  • [x] Commit history is consistent and clean, in line with the contribution guidelines.
  • [x] Make sure tests pass locally (after every commit!)

When applicable

  • [ ] When there are user facing changes: Updated documentation
  • [ ] New behavior or changes to existing untested code: Ensured that unit tests are added (See Ground Rules).
  • [ ] Large PR: Prepare changes in small commits for more convenient review
  • [ ] Bug fix: Add regression test for the bug
  • [ ] Bug fix: Create Backport PR to latest release

berland avatar Jul 03 '24 16:07 berland

import-outside-top-level (PLC0415)

Derived from the Pylint linter.

This rule is in preview and is not stable. The --preview flag is required for use.

What it does

Checks for import statements outside of a module's top-level scope, such as within a function or class definition.

Why is this bad?

PEP 8 recommends placing imports not only at the top-level of a module, but at the very top of the file, "just after any module comments and docstrings, and before module globals and constants."

import statements have effects that are global in scope; defining them at the top level has a number of benefits. For example, it makes it easier to identify the dependencies of a module, and ensures that any invalid imports are caught regardless of whether a specific function is called or class is instantiated.

An import statement would typically be placed within a function only to avoid a circular dependency, to defer a costly module load, or to avoid loading a dependency altogether in a certain runtime environment.

Example

def print_python_version():
    import platform

    print(python.python_version())

Use instead:

import platform


def print_python_version():
    print(python.python_version())

berland avatar Jul 03 '24 16:07 berland

Codecov Report

Attention: Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 90.88%. Comparing base (cf02f45) to head (a490506).

Files Patch % Lines
src/ert/__main__.py 0.00% 1 Missing :warning:
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8295   +/-   ##
=======================================
  Coverage   90.88%   90.88%           
=======================================
  Files         347      347           
  Lines       21076    21076           
=======================================
+ Hits        19154    19155    +1     
+ Misses       1922     1921    -1     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov-commenter avatar Jul 03 '24 17:07 codecov-commenter