Giter VIP home page Giter VIP logo

Comments (11)

joaoe avatar joaoe commented on June 1, 2024 3

I did comment on the subtle bugs it might introduce, but there is another question: is bugbear supposed to be a tool to help detect bugs or to check coding style/conventions ? Is there anything inherently wrong about except: pass ? Does suppress fix anything ? In the end, using suppress is more of a style choice, and perhaps not so relevant for bugbear.

from flake8-bugbear.

cooperlees avatar cooperlees commented on June 1, 2024 1

Can you please explain more about what you feel all the benefits are here please? Generally most other things bugbear lints can actually be a bug and possible cause issues. I can’t see how the try/except usage can? flake8 already lints if you don’t use the exception via ‘as’.

Is there a performance benefit or any other benefit other than the syntax feeling cleaner? This also forced people to import contextlib, where the try/except does not.

from flake8-bugbear.

joaoe avatar joaoe commented on June 1, 2024 1

Hi.
Are you proposing as a code change in bugbear's code or as a warning to be reported when analyzing code ?

I personally think the suppress() context should be used sparingly, or not used at all.

A pitfall is the following case:

with contextlib.suppress(SomeError):
  value = some_function()
do_stuff(value) # NameError: value is not defined if some_function() threw an error

or

with contextlib.suppress(SomeError):
  some_function()
  return
assert 0 # This assert runs if some_function() threw an error.

Use of suppress() can introduce such subtle bugs. Another problem is that IDE's (I use PyCharm) do not recognize that the suppress() context silences exceptions, and as such will not flag that value might not be defined.

If any, I would actually suggest adding a new warning to bugbear about the use of contextlib.suppress(), instead of encouraging its use.

from flake8-bugbear.

orlnub123 avatar orlnub123 commented on June 1, 2024 1

Django had a ticket a while back to replace all the try ... except ...: pass blocks with contextlib.suppress(...): ..., but it was reverted due to performance concerns.

Even the people commenting on the issue that introduced it were indecisive about it. I think Ezio put it best:

This saves two lines, but makes the code less explicit, it can't be used for try/except/else or try/except/except, it requires an extra import, the implementation is simple enough that it doesn't necessary need to be in the stdlib, it might encourage bad style, and it's slower.

from flake8-bugbear.

atugushev avatar atugushev commented on June 1, 2024

Hello @cooperlees,

It's more like a design solution.

from flake8-bugbear.

cooperlees avatar cooperlees commented on June 1, 2024

Hi,

Thanks for suggesting this. Due to this lint idea not fixing a bug or Python 3 compatibility like most of the other 'B' errors I don't think this fits into flake8-bugbear. It could fit in the Opinionated warnings section, but unless another flake8-bugbear collaborator likes it I am not for it. I feel the more well known try/except will be more readable to most developers, and apart from saving lines I don't see any benefits. If you actually shared more benefits etc. I would be more open, but you did not.

I will leave this open for some other developers to see and comment on. If other people think this is a big win I'd love to hear why.

from flake8-bugbear.

atugushev avatar atugushev commented on June 1, 2024

@cooperlees thanks!

from flake8-bugbear.

atugushev avatar atugushev commented on June 1, 2024

Hello @joaoe,

This is the proposal for "opinionated warnings".

Use of suppress() can introduce such subtle bugs.

AFAIK It works the same as try ... except: pass pattern, so I don't see any subtle bugs here. Try this:

try:
    value = some_function()
except SomeError:
    pass
do_stuff(value) # NameError: value is not defined if some_function() threw an error

Another problem is that IDE's (I use PyCharm) do not recognize that the suppress() context silences exceptions, and as such will not flag that value might not be defined.

Yeah, that could be a problem.

from flake8-bugbear.

atugushev avatar atugushev commented on June 1, 2024

Hello @orlnub123,

Thanks for the links! Performance matters.

from flake8-bugbear.

atugushev avatar atugushev commented on June 1, 2024

Thanks for the feedback! You've convinced me.

from flake8-bugbear.

MartinThoma avatar MartinThoma commented on June 1, 2024

People who come here might be interested in SIM105 of flake8-simplify

from flake8-bugbear.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.