Comments (17)
As Victor and Xiang noted on the original bug, this is a new feature of Python and not a bug. In this sense, linting against this would for sure create false positives. Even in your pull request you don't really suggest what the user should do (note that all other messages have suggestions). And if you suggested users to not use return-with-value
, then that's wrong. Consider def broken2
in your example. This is perfectly valid asyncio code:
def old_style_coroutine():
data = yield from get_data()
return data.some_value
How did this "bite" you?
from flake8-bugbear.
See the attached example in that bug. I wrote the return
in an actual codebase (not intentionally using any async features) because I forgot that the function used yield
further down, the function generated an empty sequence when the return
was executed. This was completely surprising to me, I was used to Python 2 catching such errors with a SyntaxError. Of course it's valid code, but you can shoot yourself in the foot with it (I did at least, it took a while to debug this). That's why lints exist.
from flake8-bugbear.
What we are trying to explain to you is that mere existence of yield
/yield from
and return-with-value
in the body of a generator is not enough for a linter to generate a valid warning. In general, bugbear has two kinds of warnings now:
B0xx - downright bugs or design issues. No excuse having code like this.
B3xx - likely valid Python 2 code that will not work on Python 3. While these can have false positives, they would trip up a human reviewer in the same way.
Your proposed warning is in a different category of "it might sometimes be a problem for people unfamiliar with a new feature of the language". You would be better served type annotating your code and running a type checker against it. The generator annotation is specifically designed for the problem you are encountering, see here:
https://www.python.org/dev/peps/pep-0484/#annotating-generator-functions-and-coroutines
from flake8-bugbear.
I'm aware that this falls into a different category. It's a quite opinionated check, and whether it has a place in a linter simply depends on what kind of linter the author wants to make. If you don't want it, that's totally fine.
Regarding type annotations -- I might as well use an actually statically typed language :)
from flake8-bugbear.
I was pondering adding a class of disabled-by-default guidance warnings. If you can figure out how we can introduce a warning that wouldn't be enabled by default unless specifically listed in your config, and creating a new class for them (say B9xx), I would be willing to merge your proposed warning.
from flake8-bugbear.
Happy to hear that! I suspect the best way to go about this are options, unless there's a Flake8-specific API I'm not aware of. @sigmavirus24 is there a way to do disabled-by-default warnings in Flake8 plugins?
from flake8-bugbear.
pycodestyle (formerly pep8) does have a bunch of disabled-by-default warning codes:
https://pycodestyle.readthedocs.io/en/latest/intro.html#error-codes
Check out how they do it.
from flake8-bugbear.
pycodestyle is not a flake8 extension as far as I can see, i.e. flake8 hooks into pycodestyle, not the other way around. It comes with its own CLI and defines its own --ignore option there.
Anyway I updated my code in #3 by reading through flake8's sourcecode. I think the used methods are supposed to be used like that, but I'm not sure. I didn't find any documentation.
from flake8-bugbear.
So Flake8 2.4+ (I think it's 2.4) allows you to annotate a check with an off_by_default
attribute that you can set to True
. 3.0+ allows you to extend the default ignore list directly instead of doing that. Depending on your compatibility matrix, depends on your solution. =)
Sorry for the delay, I'm not quite well.
from flake8-bugbear.
allows you to annotate a check with an off_by_default attribute that you can set to True
That appears to be per-plugin, we need a per-warning-code solution. Probably could still be used by defining multiple plugins + entrypoints in one package.
3.0+ allows you to extend the default ignore list directly instead of doing that.
You mean optmanager.extend_default_ignore
? That's what I currently use.
So @ambv, do we need to support Flake8 < 3.0
?
from flake8-bugbear.
Also @sigmavirus24 I respond to some mail after two months, so... which delay? 😆
Get well though.
from flake8-bugbear.
That appears to be per-plugin, we need a per-warning-code solution. Probably could still be used by defining multiple plugins + entrypoints in one package.
Correct. Hacking presently uses it with multiple plugins in the one package.
You mean optmanager.extend_default_ignore? That's what I currently use.
I hadn't looked but that is exactly what I mean. =)
from flake8-bugbear.
We don't need to support flake8 < 3.0, no.
from flake8-bugbear.
Cool, I bumped the requirement in setup.py
to flake8 >= 3.0
, now I think it's done.
from flake8-bugbear.
Hmm, flake8==3.0.4 and the latest flake8-bugbear apparently don't have this new check disabled by default! Sorry, I should've checked this before asking for review.
At first glance I can't see anything in flake8-bugbear that would be the cause of this.
from flake8-bugbear.
Are you sure? This is not my experience:
$ flake8 tests/b901.py
tests/b901.py:6:1: E302 expected 2 blank lines, found 1
$ flake8 --select=B tests/b901.py
$ flake8 --select=B901 tests/b901.py
tests/b901.py:8:9: B901: Using `yield` together with `return x`. Use native `async def` coroutines or put a `# noqa` comment on this line if this was intentional.
tests/b901.py:35:5: B901: Using `yield` together with `return x`. Use native `async def` coroutines or put a `# noqa` comment on this line if this was intentional.
The one thing that DOESN'T work is that selecting B9 still filters out B901 (you have to explicitly name each warning):
$ flake8 --select=B9 tests/b901.py
$
from flake8-bugbear.
Yeah I'm a fool, forgot that I 1.) have a setup.cfg
in my repo 2.) have a custom ignore option in there
from flake8-bugbear.
Related Issues (20)
- False positive `B023` with loop's local variable HOT 3
- B109: Documentation/error message is wrong about the violation scope HOT 2
- False-positive B902 on custom metaclass extending ABCMeta HOT 8
- Add flake8 to pre-commit config HOT 3
- B902: False positive when using the attrs library HOT 6
- Policy on bugbears in usage of popular third party libraries HOT 2
- B026 False negative on class methods HOT 1
- B017: False negative when "from" imports used HOT 2
- Improve unit test runner HOT 2
- Feature request: a rule for calling super().__init__() in custom exception's __init__() HOT 2
- Feature request: a rule for detecting calls to mutate all-caps "constants" HOT 1
- B035: False positive for comprehensions that use a walrus operator
- Couple new rule suggestions HOT 2
- Rule to detect changes to iterable object of loop HOT 5
- Error in latest version of flake8-bugbear 24.1.15 HOT 4
- B038 false positive in 24.1.15 + 24.1.16 HOT 5
- B018 doesn't trigger for useless expressions involving multiple variables HOT 3
- B909 improvements HOT 4
- B038 false positives HOT 5
- How to handle B015 within pytest.raises blocks
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flake8-bugbear.