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

kwargs used by decorator being flagged as unused. #3662

Open
rtaft opened this issue Jun 2, 2020 · 3 comments
Open

kwargs used by decorator being flagged as unused. #3662

rtaft opened this issue Jun 2, 2020 · 3 comments
Labels
Bug 🪲 Decorators False Positive 🦟 A message is emitted but nothing is wrong with the code Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine

Comments

@rtaft
Copy link

rtaft commented Jun 2, 2020

After upgrading from pylint 2.4.4 to 2.5.2, we started getting unused-argument errors with kwargs that are sent to decorators for Marshmallow. What is weird is that it is not consistent. In my codebase, I encountered these and added this to the disabled list where I was seeing the lint error to ignore it. However there are many other places in the codebase that we use Marshmallow decorators that did not error for me, and other developers are seeing errors on some (but not all) of those.

Steps to reproduce

  1. Create and lint a Marshmallow Schema that utilizes decorators like validates_schema
from marshmallow import fields, Schema, validate, validates_schema, ValidationError

class TestSchema(Schema):
    test_url = fields.Str(required=True, validate=validate.Length(max=1000))

    @validates_schema
    def validate_test_url(self, data, **kwargs):
        if data['test_url'] and not data['test_url'].startswith('https://test.example.com'):
            raise ValidationError('URL must start with {}'.format('https://test.example.com'))

Current behavior

[W0613(unused-argument), TestSchema.validate_test_url] Unused argument 'kwargs'

Expected behavior

Passes without error

pylint --version output

pylint 1.9.5,
astroid 1.6.6
Python 2.7.17 (default, Apr 15 2020, 17:20:14)
[GCC 7.5.0]

@Pierre-Sassoulas
Copy link
Member

I can reproduce the problem with marshmallow==3.10.0. I tried to make a functional test without importing marshmallow code but this is taking too long for me.

@rmorshea
Copy link

Does pylint ignore unused _ variables? If so, you might be able to use **_.

@Pierre-Sassoulas Pierre-Sassoulas added the Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine label Feb 24, 2023
@AAkines
Copy link

AAkines commented Aug 31, 2023

Seeing this issue, as well. Per marshmallow decorator docs, many validation funcs take **kwargs, assumedly to handle partials passed to #validator_func() in schema.py

Unless a use case is known for **kwargs, functionality remains the same with **_kwargs used instead. This is not an acceptable solution, given this must be applied to all schema funcs using **kwargs. An adjustment for variable argument linting in pylint is preferred, instead. Example error stack below

Traceback (most recent call last):
  File "some_project_path-py3.11/lib/python3.11/site-packages/marshmallow/schema.py", line 722, in load
    return self._do_load(
           ^^^^^^^^^^^^^^
  File "some_project_path-py3.11/lib/python3.11/site-packages/marshmallow/schema.py", line 884, in _do_load
    self._invoke_schema_validators(
  File "some_project_path-py3.11/lib/python3.11/site-packages/marshmallow/schema.py", line 1185, in _invoke_schema_validators
    self._run_validator(
  File "some_project_path-py3.11/lib/python3.11/site-packages/marshmallow/schema.py", line 774, in _run_validator
    validator_func(output, partial=partial, many=many)
TypeError: SomeSchema.validate_func() got an unexpected keyword argument 'partial'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Decorators False Positive 🦟 A message is emitted but nothing is wrong with the code Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine
Projects
None yet
Development

No branches or pull requests

5 participants