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

Rewriting to remove monkey patching ? #263

Open
mocquin opened this issue Aug 17, 2024 · 3 comments
Open

Rewriting to remove monkey patching ? #263

mocquin opened this issue Aug 17, 2024 · 3 comments

Comments

@mocquin
Copy link

mocquin commented Aug 17, 2024

The source code of uncertainties is filled with monkey patching statements, are their any initiative to rewrite it ?

I understand the "only" benefit of this is having more readable code (easier to understand for newcomers, understand the API and overall package just by reading the code).

@wshanks
Copy link
Collaborator

wshanks commented Aug 17, 2024

There is discussion in several recent issues/discussions about refactoring in general. There is a lot of dynamic code in the package. I am not sure which parts you are classifying as monkey patching. I think it might always make sense to generate uncertainties wrapped versions of standard math functions dynamically rather than write them all out separately (unless the Python file is generated dynamically itself as part of packaging so that at runtime it is static).

Often, monkey-patching is used to describe more specifically a project dynamically modifying code outside of itself at runtime. A common case is a web browser plugin changing the javascript on a web page. For Uncertainties, this would mean changing the behavior of Numpy functions or the math module in the standard library. As far as I know Uncertainties does not do that. It only modifies its own code as part of being imported.

@jagerber48
Copy link
Contributor

Monkey patching happens in at least two places.

  • AffineScalarFunc gets various float functions via monkey patching after wrapping the float functions in wrappers for handling inputs (type casting, nan handling) and for the actual uncertainties propagation involving calculating derivatives and returning new uncertain objects.
  • The umath module is patched with wrapped versions of math module functions.

The monkey patches occur because many, at least >10 functions are being patched into both of these places. And the patching happens by applying very similar wrappers to the various functions, so hard coding the functions into the source code would just be a lot of boilerplate and would introduce more room for oversights.

That said, I do have a re-write branch where, among other things, I try to make the monkey-patching a bit easier to follow See the PR on my fork here: jagerber48#2. Here's what I do to make the monkey patching more readable:

Note that there aren't currently plans to merge this branch into master. Right now I'm just using it as a sandbox/demonstration of what the code could look like to inform more concrete maintenance plans.

@newville
Copy link
Member

@mocquin Just to kind of follow up on @wshanks and @jagerber48 comments:

You say "monkey-patching" as if that is a bad thing ;). Um, is it? It is kind of a feature of Python.

It is also kind of a vague term, and you don't point to any actual occurrences - @jagerber48 does so, nicely, and also to the very promising re-factoring.

As you can see from the recent re-location and the lengthy GitHub Discussions and Issues conversations over the past several months, there is a lot of discussion about how to refactor this code into something that is both more modern (we may have gotten rid of all the Python2 code at this point), and easier for the new maintainers to understand.

We would love more help. But if you mean "rewrite it because monkey-patching is bad", then I would say that we would love help improving the quality of the code, whether that means removing monkey-patching, adding monkey-patching, or other improvements.

You say:

I understand the "only" benefit of this is having more readable code (easier to understand for newcomers, understand the API and overall package just by reading the code).

Understandable code is indeed a high priority. I cannot tell whether you mean "monkey-patching makes code more readable" or "removing monkey-patching makes code more readable". But for any case where either statement is true, more readable code should carry weight.

I am skeptical of development approaches that insist some feature of the language is always bad and should never be used.

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

No branches or pull requests

4 participants