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

README.rst: recommend python -OO compatible usage #515

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cben
Copy link

@cben cben commented Aug 15, 2023

The current recommended recipe using docstring syntax is incompatible with python -OO mode.
TypeError: expected string or bytes-like object
https://discuss.python.org/t/stop-ignoring-asserts-when-running-in-optimized-mode/13132/31

Who cares?

  • -OO slightly reduces RAM usage.
  • If one distributes a script as a self-contained executable, it's tempting to precompile .pyo with docstring stripping for reduced dependencies size. But any files using docopt as recommended will stop working.
  • It's an easily avoidable "papercut".
  • Finally, I wish every tool relying on docstrings for functionality (which is cool 👍) at least mentioned the existance of -OO...

This PR mentions the problem in the README, and modifies the default recipe so it's -OO-safe.


  • There is a shorter recipe that happens to work with -OO:

    __doc__ = """Naval Fate.
    ...
    """
    

    That's it! Because syntactically it's an assignment statement, not native docstring syntax, it doesn't get stripped. And it executes after __doc__ is initialized to None by (lack of) native docstring syntax, so no conflict.

    But that feels too "magic" to me (and really requires a comment for the next programmer to not remove it). IMHO the explicit use of a different variable DOC = ... is clearer, and highlights the independent goals of:

    1. docopt() — which can take whatever string you give it;
    2. pydoc / help() / other introspection which may look at .__doc__.

    Which do you think is better to document?

The current recommended recipe using docstring syntax is
incompatible with python -OO mode.
https://discuss.python.org/t/stop-ignoring-asserts-when-running-in-optimized-mode/13132/31

Who cares?
- `-OO` slightly reduces RAM usage.
- If one distributes a script as a self-contained executable,
  it's tempting to precompile .pyo with docstring stripping
  for reduced dependencies size.  But any files using docopt
  as recommended will stop working.
- It's an easily avoidable "papercut".

This mentions the problem in the README, and modifies the default recipe so it's `-OO`-safe.

* There is a shorter recipe that also works with -OO:
  
      __doc__ = """Naval Fate.
     ...
     """

  That one feels too "magic" to me though, and really needs
  a comment for the next programmer to not remove it.
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.

None yet

1 participant