Skip to content

Fix AttributeError in get_scope when inspect.getmodule() returns None#187

Merged
samwaseda merged 5 commits intosamwaseda-patch-1from
copilot/sub-pr-186
Mar 18, 2026
Merged

Fix AttributeError in get_scope when inspect.getmodule() returns None#187
samwaseda merged 5 commits intosamwaseda-patch-1from
copilot/sub-pr-186

Conversation

Copy link
Contributor

Copilot AI commented Mar 18, 2026

Summary

get_scope called inspect.getmodule(func).__dict__ unconditionally. With the parameter type widened to object, inspect.getmodule() returning None (builtins, dynamically-created objects) became easy to trigger, raising an opaque AttributeError on NoneType.

Related Issue(s)

Addresses review feedback on #186.

Backward Compatibility

No breaking changes. The function previously crashed with AttributeError in the None case; it now raises a descriptive ValueError instead. Normal callsites are unaffected.

Detailed Description

get_scope now:

  1. Checks the result of inspect.getmodule(func) for None
  2. If None, falls back via getattr(func, "__module__", None) + sys.modules.get()
  3. If the module still cannot be resolved, raises a clear ValueError
# Previously: AttributeError: 'NoneType' object has no attribute '__dict__'
# Now: ValueError with a descriptive message
import types, sys

dynamic_func = types.FunctionType((lambda: None).__code__, {}, "_orphan")
dynamic_func.__module__ = None
get_scope(dynamic_func)  # raises ValueError: Cannot determine the module for ...

# Fallback path works when __module__ names a loaded module
mod = types.ModuleType("_my_mod")
sys.modules["_my_mod"] = mod
dynamic_func.__module__ = "_my_mod"
get_scope(dynamic_func)  # resolves correctly via sys.modules

Two new tests cover the fallback path and the ValueError case. Code has been formatted with black. flowrep/_version.py (a setuptools-scm build artifact) was also removed from version control and added to .gitignore to prevent it from being accidentally committed in the future.


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

…lear ValueError

Co-authored-by: samwaseda <37879103+samwaseda@users.noreply.github.com>
Copilot AI changed the title [WIP] [WIP] Address feedback on get_scope parameter type change in PR #186 Fix AttributeError in get_scope when inspect.getmodule() returns None Mar 18, 2026
Copilot AI requested a review from samwaseda March 18, 2026 11:02
@samwaseda
Copy link
Member

@copilot Remove the _version.py

@samwaseda
Copy link
Member

@copilot run black

@codecov
Copy link

codecov bot commented Mar 18, 2026

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.92%. Comparing base (66ff2ae) to head (1f66017).
⚠️ Report is 6 commits behind head on samwaseda-patch-1.

Files with missing lines Patch % Lines
flowrep/models/parsers/object_scope.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@                  Coverage Diff                  @@
##           samwaseda-patch-1     #187      +/-   ##
=====================================================
- Coverage              97.96%   97.92%   -0.04%     
=====================================================
  Files                     34       34              
  Lines                   2260     2268       +8     
=====================================================
+ Hits                    2214     2221       +7     
- Misses                    46       47       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…gitignore

Co-authored-by: samwaseda <37879103+samwaseda@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 18, 2026

@copilot Remove the _version.py

Done in 539fe52 — removed flowrep/_version.py and added _version.py to .gitignore to prevent it from being committed again.

Co-authored-by: samwaseda <37879103+samwaseda@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 18, 2026

@copilot run black

Done in 1f8b6e4 — black reformatted tests/unit/models/parsers/test_object_scope.py (1 file changed).

@samwaseda samwaseda marked this pull request as ready for review March 18, 2026 11:36
Copilot AI requested a review from samwaseda March 18, 2026 11:36
@samwaseda samwaseda merged commit 62fa0e4 into samwaseda-patch-1 Mar 18, 2026
17 of 19 checks passed
@samwaseda samwaseda deleted the copilot/sub-pr-186 branch March 18, 2026 12:42
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