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

Add Flake8-BugBear and Bandit (Security!) #637

Merged
merged 13 commits into from
Apr 18, 2024
Merged

Conversation

juanitorduz
Copy link
Collaborator

@juanitorduz juanitorduz commented Apr 16, 2024

Let me know what you think about it :)


📚 Documentation preview 📚: https://pymc-marketing--637.org.readthedocs.build/en/637/

@ricardoV94
Copy link
Contributor

ricardoV94 commented Apr 16, 2024

The missing asserts are embarrassing.
The zip is fine

Not convinced by the raises and the stacklevels

@juanitorduz
Copy link
Collaborator Author

juanitorduz commented Apr 16, 2024

The missing asserts are embarrassing.

This is why I like this ! haha

Not convinced by the raises and the stacklevels

I can #noqa these changes :)

Copy link

codecov bot commented Apr 16, 2024

Codecov Report

Attention: Patch coverage is 80.00000% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 91.58%. Comparing base (582d2ed) to head (7e06893).

Files Patch % Lines
pymc_marketing/mmm/base.py 0.00% 1 Missing ⚠️
pymc_marketing/mmm/delayed_saturated_mmm.py 0.00% 1 Missing ⚠️
pymc_marketing/mmm/transformers.py 0.00% 1 Missing ⚠️
pymc_marketing/model_builder.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #637      +/-   ##
==========================================
- Coverage   91.62%   91.58%   -0.05%     
==========================================
  Files          22       22              
  Lines        2280     2281       +1     
==========================================
  Hits         2089     2089              
- Misses        191      192       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ricardoV94
Copy link
Contributor

ricardoV94 commented Apr 16, 2024

I can #noqa these changes :)

The raises are very opinionated, it looked like the code was specifically looking for the exception and converting it to something else for the user. I don't know of noqa makes more sense than disabling altogether

The stacklevel I simply don't get. Isn't 1 the default?

@ricardoV94
Copy link
Contributor

Also for the zip, is it forcing a default or asking you to be explicit?

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@juanitorduz
Copy link
Collaborator Author

Also for the zip, is it forcing a default or asking you to be explicit?

Asking to be explicit

The raises are very opinionated, it looked like the code was specifically looking for the exception and converting it to something else for the user. I don't know of noqa makes more sense than disabling altogether

I do not have a strong opinion here. Maybe we can simply ignore that rule.

The stacklevel I simply don't get. Isn't 1 the default?

The stacklevel needs to be explicit to I am putting the default. Qw can #noqa these ones.

WDYT?

@juanitorduz juanitorduz changed the title Add Flake8-BugBear Add Flake8-BugBear and Bandit (Security!) Apr 16, 2024
@wd60622
Copy link
Contributor

wd60622 commented Apr 16, 2024

I can #noqa these changes :)

The raises are very opinionated, it looked like the code was specifically looking for the exception and converting it to something else for the user. I don't know of noqa makes more sense than disabling altogether

The stacklevel I simply don't get. Isn't 1 the default?

It might be the default, but isn't it recommended to have at least 2 or more? else nothing shows up?

@wd60622
Copy link
Contributor

wd60622 commented Apr 16, 2024

The missing asserts are embarrassing.

Think I am the culprit for a few a these 🙈

@juanitorduz
Copy link
Collaborator Author

I can #noqa these changes :)

The raises are very opinionated, it looked like the code was specifically looking for the exception and converting it to something else for the user. I don't know of noqa makes more sense than disabling altogether
The stacklevel I simply don't get. Isn't 1 the default?

It might be the default, but isn't it recommended to have at least 2 or more? else nothing shows up?

Copilot suggests 2 XD so we can use that and not the default hehe

@juanitorduz
Copy link
Collaborator Author

The missing asserts are embarrassing.

Think I am the culprit for a few a these 🙈

This is why I like this plugin, I did it many times and probably will. So I need a system to prevent this haha

@ricardoV94
Copy link
Contributor

the level depends on what you want the user to see as the source of the warning. It's usually one, but sometimes it's called by a helper or a very internal function and 2 (or 3 or whatever) is more appropriate. I don't think 2 is recommended blindly?

@juanitorduz
Copy link
Collaborator Author

So then we can take it case by case. This is good imo to have to think about this :)

@juanitorduz
Copy link
Collaborator Author

the level depends on what you want the user to see as the source of the warning. It's usually one, but sometimes it's called by a helper or a very internal function and 2 (or 3 or whatever) is more appropriate. I don't think 2 is recommended blindly?

I suggest we leave stacklevel=1 as the warnings are raised in public methods.

@juanitorduz
Copy link
Collaborator Author

Personally, I like to know where the errors are coming from so I am in for keeping the from err addition to the warnings. @ricardoV94 , you have a better experience with user-facing errors, so if you think these are generally more confusing for the user, then I will ignore the result and revert the changes 🙏

@juanitorduz juanitorduz marked this pull request as ready for review April 17, 2024 09:38
@ricardoV94
Copy link
Contributor

Personally, I like to know where the errors are coming from so I am in for keeping the from err addition to the warnings. @ricardoV94 , you have a better experience with user-facing errors, so if you think these are generally more confusing for the user, then I will ignore the result and revert the changes 🙏

Some errors are being expected/ handled by us, so there's not much to gain from passing the original error. No strong opinion though.

In this example, I don't see what's the point of raising the original exception. The user may have no idea about how PyMC model contexts work, and I would guess the whole trace-back would be more confusing than our curated error.

def user_facing_foo():
  # This function must be called in a model context.
  try:
    pm.model_context(None)
  except ModelContextError:   # or whatever
    raise RuntimeError("You must call this function from within a PyMC model")

@juanitorduz
Copy link
Collaborator Author

juanitorduz commented Apr 17, 2024

Yep! That makes sense! Then let's revert and ignore this rule and make sure we think about the user when reporting errors (as we have been doing) 🙌

@juanitorduz
Copy link
Collaborator Author

@ricardoV94 done ✅

@ricardoV94
Copy link
Contributor

ricardoV94 commented Apr 18, 2024

Btw, my last comments are mostly nitpicks. Just wanted to emphasize those assert is not None were by design not errors to avoid code duplication, and needed only because mypy is too retarded to analyze type flow across function calls.

Basically the situation was

@property
def fit_result(self):
    if self.idata is None or "posterior" not in idata.groups:  # or whatever
       raise ValueError(...)
   return self.idata.posterior

def foo(self):
    # Raise informative error
    self.fit_result
    # Use self.idata (or self.fit_result, which mypy would not have complained abuot I guess)
    self.idata...  # mypy complains it may be None

@juanitorduz
Copy link
Collaborator Author

Thanks for the feedback and comments @ricardoV94 ! Could you please see if you agree with the changes 🙏 ?

@juanitorduz
Copy link
Collaborator Author

@ricardoV94 It was my first time using the := operator 😆

@ricardoV94
Copy link
Contributor

@ricardoV94 It was my first time using the := operator 😆

Me too haven't found a good use for it yet

Copy link
Contributor

@wd60622 wd60622 left a comment

Choose a reason for hiding this comment

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

Comment on the fit_result attribute already causing an error to raise

pymc_marketing/clv/models/gamma_gamma.py Outdated Show resolved Hide resolved
Copy link
Contributor

@wd60622 wd60622 left a comment

Choose a reason for hiding this comment

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

Was commenting same as ricardo
Looks good to me!

@juanitorduz juanitorduz merged commit c02d349 into main Apr 18, 2024
9 of 10 checks passed
@juanitorduz juanitorduz deleted the add_flake8_bugbear branch April 18, 2024 18:04
@juanitorduz
Copy link
Collaborator Author

Thank you @wd60622 and @ricardoV94 🎸 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants