Menu

#430 avoid mutables as default functions arguments

None
closed-fixed
rst parser (3)
2022-07-06
2021-10-25
No

Mutables shouldn't be used as default parameters, it is a Python anti-pattern. See for example:
* Default Parameter Values in Python
* Python Mutable Defaults Are The Source of All Evil
* The Hitchhiker's Guide to Python - Common Gotchas
* Python Pitfall: Mutable Default Arguments

Mutable default arguments are used in a few places in docutils. Just grep =[] and ={} and retain function definitions.

Mutable default arguments can also be found in the documentation:
https://docutils.sourceforge.io/docs/howto/rst-roles.html#define-the-role-function
https://docutils.sourceforge.io/docs/howto/rst-roles.html#rfc-reference-role

Of course, that's not an immediate problem if the mutable arguments are actually not mutated by the function, but it remains an anti-pattern that may create problems in the future.

Discussion

  • Dimitri Papadopoulos

    Attached is a suggestion. I would agree it's more verbose and perhaps more complex to understand. But then using anti-patterns in documentation looks like a bad idea.

     
  • Günter Milde

    Günter Milde - 2021-11-02

    Thank you for the report and proposal.
    Attached is a patch against revision 8874 in the code repository.
    As the bug may also be considered just a styling issue and the patch has the potential to break compatibility for 3rd party users, it will have to wait for review and discussion on the way ahead
    (inclusion no earlier than 0.19 or 1.0).

     
  • Günter Milde

    Günter Milde - 2021-11-05
    • labels: --> rst parser
    • assigned_to: Günter Milde
     
  • Günter Milde

    Günter Milde - 2021-12-29
    • status: open --> open-fixed
     
  • Günter Milde

    Günter Milde - 2021-12-29

    Fixed in [r8919]. Thank you for the report.

     

    Related

    Commit: [r8919]


    Last edit: Günter Milde 2022-07-06
  • Günter Milde

    Günter Milde - 2022-07-06
    • status: open-fixed --> closed-fixed
     
  • Günter Milde

    Günter Milde - 2022-07-06

    Fixed in Docutils 0.19.
    Thank you for the report.

     

Log in to post a comment.

MongoDB Logo MongoDB