Pylint: Add option to ignore some "redefined-outer-name"

Created on 13 Jun 2017  Â·  5Comments  Â·  Source: PyCQA/pylint

This is mainly related to Pytests fixtures where fixtures always raise "redefined-outer-name" error.

A setting could be added to ignore some names based on regex.

Some possibilities :

# basic
ignore-redefined-outer-name=fixture_abs,fixtures_foo,fixture_bar
# "start by"
ignore-redefined-outer-name=fixture_
# regex
ignore-redefined-outer-name-rgx=fixture_[a-z]$

Example or problem :

import smtplib
import pytest


@pytest.fixture
def fixture_smtp():
    """Pytest fixture"""
    return smtplib.SMTP("smtp.gmail.com")


def test_ehlo(fixture_smtp):
    """Example"""
    response, msg = fixture_smtp.ehlo()
    assert response == 42
    assert msg == 'foo'

Result :

➜  tmp pylint example.py 
No config file found, using default configuration
************* Module example
W: 14,14: Redefining name 'fixture_smtp' from outer scope (line 9) (redefined-outer-name)

[...]

Messages
--------

+---------------------+------------+
|message id           |occurrences |
+=====================+============+
|redefined-outer-name |1           |
+---------------------+------------+

Global evaluation
-----------------
Your code has been rated at 8.75/10
enhancement

Most helpful comment

I'm not working anymore on this project so I can't tell you if it's fine but thank you !
@gustavi

Correct:

@pytest.fixture(name="smtp")
def fixture_smtp:
    """Pytest fixture"""
    return smtplib.SMTP("smtp.gmail.com")

def test_ehlo(smtp):
    """Example"""
    response, msg = smtp.ehlo()
    assert response == 42
    assert msg == 'foo'

Add name parameter for fixture and fixture_ prefix in def.

All 5 comments

This sounds good to me. If anyone has time to tackle this and send a PR, that would be definitely appreciated.

I'll send a PR in next days/weeks.

A solution for this is to use the name attr of the fixture decorator:

@pytest.fixture
def fixture_smtp(name="smtp"):
    """Pytest fixture"""
    return smtplib.SMTP("smtp.gmail.com")

def test_ehlo(smtp):
    """Example"""
    response, msg = smtp.ehlo()
    assert response == 42
    assert msg == 'foo'

(I personally like to avoid pylint-disables where there is another clean option)

I'm not working anymore on this project so I can't tell you if it's fine but thank you !

I'm not working anymore on this project so I can't tell you if it's fine but thank you !
@gustavi

Correct:

@pytest.fixture(name="smtp")
def fixture_smtp:
    """Pytest fixture"""
    return smtplib.SMTP("smtp.gmail.com")

def test_ehlo(smtp):
    """Example"""
    response, msg = smtp.ehlo()
    assert response == 42
    assert msg == 'foo'

Add name parameter for fixture and fixture_ prefix in def.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pylint-bot picture pylint-bot  Â·  3Comments

PCManticore picture PCManticore  Â·  3Comments

jrial picture jrial  Â·  3Comments

DGalt picture DGalt  Â·  3Comments

DevynCJohnson picture DevynCJohnson  Â·  3Comments