wemake-python-styleguide icon indicating copy to clipboard operation
wemake-python-styleguide copied to clipboard

Do not allow `super()` without parameters in generator expressions

Open sobolevn opened this issue 3 years ago • 16 comments

This bug: https://bugs.python.org/issue46175

We need to require super(cls, self) in this case.

sobolevn avatar Dec 26 '21 14:12 sobolevn

Hi ! I would like to work on this issue :)

ghost avatar Jan 03 '22 13:01 ghost

@nasdev-cmd thank you!

sobolevn avatar Jan 03 '22 13:01 sobolevn

Hello, is this issue still open? If so, I would appreciate it greatly if I could provided with the information of where is the code linked to this behavior located.

kitsiosvas avatar Jan 26 '22 23:01 kitsiosvas

@kitsiosvas I think that we can add this check here: https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/visitors/ast/classes.py

Are you familiar with python's AST?

sobolevn avatar Jan 27 '22 06:01 sobolevn

@sobolevn I am somewhat familiar with AST indeed. Are there any other similar checks that I could consult? I've been studying the code you included in your previous message to understand what exactly I will need to implement. Thank you.

kitsiosvas avatar Jan 27 '22 11:01 kitsiosvas

You can take a look at this: https://github.com/wemake-services/wemake-python-styleguide/blob/61d4fa55c7898bb124ccf573219e8ccaac543742/wemake_python_styleguide/visitors/ast/functions.py#L95-L102

It just checks for wrong functions. But, in this case you will also need the context. See https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/logic/walk.py

sobolevn avatar Jan 27 '22 12:01 sobolevn

If I understand it correctly, I need to somehow check if super() exists as a node with parent a generator expression and it's kids are not in the form you have pointed out initially (super(cls, self)). I don't know if I am missing something. Any input is more than welcome.

kitsiosvas avatar Jan 27 '22 13:01 kitsiosvas

Yes, sounds right!

If super call:

  • is inside a gen expr
  • and does not have two args

We raise a violation.

sobolevn avatar Jan 27 '22 13:01 sobolevn

@sobolevn Thank you for your quick reply. I am having trouble figuring out how do I check if a node is inside a generator expression. Is there a tactic that I am missing? Sorry for making this threat a long one.

Edit: I am thinking of something like using get_closest_parent(node, parents) method. As first argument there will be the super() call node, and as second argument I will have to include generator expressions (not sure on how that is implemented). If it returns null then super() is not in a gen expr.

kitsiosvas avatar Jan 27 '22 13:01 kitsiosvas

I would go this way:

  1. Find ast.Call nodes, check name to be super
  2. Check args count (it is fast and cheap)
  3. Then get_closest_parent(call, ast.GeneratorExpr)

sobolevn avatar Jan 27 '22 14:01 sobolevn

In order to get the node's name I would have to use given_function_called(node, to_check: Container[str]) method. What would I use as Container in this case. I couldn't find anything clear on this. Or maybe there is another way of getting the name of the node?

kitsiosvas avatar Jan 27 '22 15:01 kitsiosvas

given_function_called(node, {'super'})

sobolevn avatar Jan 27 '22 15:01 sobolevn

I have implemented the feature. Should I issue a Pull Request for review?

kitsiosvas avatar Jan 27 '22 17:01 kitsiosvas

Please! 👍

sobolevn avatar Jan 27 '22 17:01 sobolevn

According to the "Raise a violation" part, I tried doing something similar to https://github.com/wemake-services/wemake-python-styleguide/issues/2310#issuecomment-1023135723 , but it raises errors apparently. I couldn't figure out what is the proper way of achieving this, so far. Will keep searching, but any help is amazing. Thank you.

kitsiosvas avatar Jan 27 '22 20:01 kitsiosvas

does this issue still exists , I want to contribute in this project , actually its my first opensource contribution , so any help regarding this really appreciated

akshaybhadange avatar Mar 13 '22 17:03 akshaybhadange

Can I take this issue over?

lensvol avatar Oct 12 '22 11:10 lensvol

Please! Can you also check other comprehensions? And nested functions?

sobolevn avatar Oct 12 '22 11:10 sobolevn

I'll do what I can.

lensvol avatar Oct 12 '22 12:10 lensvol