Skip to content

Updated ForbiddenImportChecker to infer import argument values - #1398

Open
angelayzheng wants to merge 2 commits into
pyta-uoft:masterfrom
angelayzheng:forbidden-import-checker-args
Open

angelayzheng wants to merge 2 commits into
pyta-uoft:masterfrom
angelayzheng:forbidden-import-checker-args

Conversation

@angelayzheng

@angelayzheng angelayzheng commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Proposed Changes

Updated ForbiddenImportChecker to use astroid inference for the visit_call method, which checks calls to the __import__ dunder function.

This allows module names that are not passed in as string literals to be parsed correctly, such as __import__('ma' + 'th').

...

Screenshots of your changes (if applicable)

Type of Change

(Write an X or a brief description next to the type or types that best describe your changes.)

Type Applies?
🚨 Breaking change (fix or feature that would cause existing functionality to change)
✨ New feature (non-breaking change that adds functionality)
🐛 Bug fix (non-breaking change that fixes an issue) x
♻️ Refactoring (internal change to codebase, without changing functionality)
🚦 Test update (change that only adds or modifies tests)
📚 Documentation update (change that only updates documentation)
📦 Dependency update (change that updates a dependency)
🔧 Internal (change that only affects developers or continuous integration)

Checklist

(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the [ ] into a [x] in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)

Before opening your pull request:

  • I have performed a self-review of my changes.
    • Check that all changed files included in this pull request are intentional changes.
    • Check that all changes are relevant to the purpose of this pull request, as described above.
  • I have added tests for my changes, if applicable.
    • This is required for all bug fixes and new features.
  • I have updated the project documentation, if applicable.
    • This is required for new features.
  • I have updated the project Changelog (this is required for all changes).
  • If this is my first contribution, I have added myself to the list of contributors.

After opening your pull request:

  • I have verified that the CI tests have passed.
  • I have reviewed the test coverage changes reported by Coveralls.
  • I have requested a review from a project maintainer.

Questions and Comments

  • How should the new visit_call method handle arguments that cannot be parsed as strings, such as the line __import__(12)? Currently, a direct check using visit_call would return early without raising any errors, but should it add a message or raise an error?
    • Python usually raises TypeError: module name must be a string if __import__(12) is run
    • The previous version of ForbiddenImportChecker would raise TypeError: can only concatenate str (not "int") to str when args = ("module " + node.args[0].value,) was run (a bug)
    • After the correct approach is decided, I will add a test covering this case.

@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 36159213112

Coverage decreased (-0.02%) to 90.894%

Details

  • Coverage decreased (-0.02%) from the base build.
  • Patch coverage: 1 uncovered change across 1 file (5 of 6 lines covered, 83.33%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
packages/python-ta/src/python_ta/checkers/forbidden_import_checker.py 6 5 83.33%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 4085
Covered Lines: 3713
Line Coverage: 90.89%
Coverage Strength: 17.67 hits per line

💛 - Coveralls

This branch has not been deployed

No deployments
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.

2 participants