Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function inspection of functools.partials in migration files #247

Merged

Conversation

casparwylie
Copy link

@casparwylie casparwylie commented Feb 3, 2023

Django migrations seem to include the use of functools.partial, though such objects do not carry the __name__ property, hence the errors

AttributeError: 'functools.partial' object has no attribute '__name__'

when running the linter on migrations that use functools.partial.

and for source code inspection...

n_linter.py", line 545, in get_runpython_model_import_issues
    source_code = inspect.getsource(code)
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/inspect.py", line 1270, in getsource
    lines, lnum = getsourcelines(object)
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/inspect.py", line 1252, in getsourcelines
    lines, lnum = findsource(object)
                  ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/inspect.py", line 1063, in findsource
    file = getsourcefile(object)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/inspect.py", line 940, in getsourcefile
    filename = getfile(object)
               ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/inspect.py", line 920, in getfile
    raise TypeError('module, class, method, function, traceback, frame, or '
TypeError: module, class, method, function, traceback, frame, or code object was expected, got partial

@casparwylie casparwylie marked this pull request as draft February 7, 2023 12:33
@casparwylie casparwylie marked this pull request as ready for review February 7, 2023 12:33
@David-Wobrock
Copy link
Collaborator

Hey @casparwylie 👋

Thanks for taking interest in the project, and taking the time to submit a PR!

Would you mind sharing an example of a data migration (RunPython) that is a partial function? :) I'm curious about the use case, and it will help us writing a test case for this logic.

Cheers!

@David-Wobrock David-Wobrock self-assigned this Feb 19, 2023
@casparwylie
Copy link
Author

No problem! So I've seen it around in some old migration code, and actually don't have much context myself, however I found this online as an example https://code.djangoproject.com/ticket/26475.

@codecov-commenter
Copy link

codecov-commenter commented Jul 3, 2023

Codecov Report

Patch coverage: 96.42% and project coverage change: +0.01 🎉

Comparison is base (97a1f5c) 93.95% compared to head (6ba6102) 93.97%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #247      +/-   ##
==========================================
+ Coverage   93.95%   93.97%   +0.01%     
==========================================
  Files          77       78       +1     
  Lines        1969     1992      +23     
==========================================
+ Hits         1850     1872      +22     
- Misses        119      120       +1     
Impacted Files Coverage Δ
...ata_migrations/migrations/0004_partial_function.py 90.00% <90.00%> (ø)
django_migration_linter/migration_linter.py 85.45% <100.00%> (+0.30%) ⬆️
tests/functional/test_data_migrations.py 79.34% <100.00%> (+0.57%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@David-Wobrock David-Wobrock merged commit 2d15ea8 into 3YOURMIND:main Jul 3, 2023
7 checks passed
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