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

Move manage.py content to evap.__main__ #2387

Merged
merged 3 commits into from
Feb 10, 2025

Conversation

niklasmohrin
Copy link
Member

@niklasmohrin niklasmohrin commented Feb 10, 2025

This is in preparation for #2328, where we want to include the manage.py code, but cannot include the manage.py file because it is outside the evap module. Using __main__ allows running management commands with python -m evap if evap is installed into the environment. Running manage.py still works, as it uses the builtin runpy module to simulate running python -m evap.

This allows running management commands with `python -m evap` if evap is installed into the environment.
evap/__main__.py Outdated Show resolved Hide resolved
manage.py Outdated

settings.DATADIR.mkdir(exist_ok=True)
execute_from_command_line(sys.argv)
main()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that anyone would ever import this file, but is there a specific reason to remove the if __name__ == "__main__" guard?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason except brevity

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add it back if you want

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean ideally, this would be python -m evap directly... What about https://docs.python.org/3/library/runpy.html#runpy.run_module?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runpy sure is ... something :D It fixes the missing coverage, but it adds some complexity that we might not want. @richardebeling ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the right thing to do to me. I'm not sure I get your points about missing coverage (you mean test coverage? Would that have been a problem?) and complexity (import runpy and runpy.run_module look canonical to me, I wouldn't care what they do under the hood -- do I need to care?), unless I'm missing something here I'm very fine with the current code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering whether runpy magic could break something like ./manage.py test --pdb, but it seems to work just fine. My points are

  • without runpy, the line in __main__.py that calls main is not covered, but with runpy, it is (although the branch where __name__ is not __main__ is uncovered now :D)
  • I also don't care what runpy does and don't want to care, I was just afraid that we would have to care some day. It also makes the manage.py file less approachable to first semester students, but I guess they have to accept our arcane magic anyways once they hit execute_from_command_line

I also think runpy is fun, so we can keep it if the above don't concern you :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, makes sense. I'm fine with both approaches.

@niklasmohrin niklasmohrin mentioned this pull request Feb 10, 2025
Copy link
Collaborator

@Kakadus Kakadus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both runpy or not is fine

@niklasmohrin
Copy link
Member Author

I noticed that the usage text is sometimes messed up, for example python -m evap test --help prints usage: __main__.py test [snip]. Normally, Django wants to cover this up, but it doesn't seem to work with subcommands. Either way, the "fixed" variant would be usage: python -m django test [snip], which would also be wrong. If we were to really care about this, we would have to reimplement execute_from_command_line ourselves. I, personally, don't care

@niklasmohrin niklasmohrin merged commit 5404511 into e-valuation:main Feb 10, 2025
12 of 13 checks passed
@niklasmohrin niklasmohrin deleted the dunder-main branch February 10, 2025 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants