Enable Ruff's pylint 'PLC2201' check

Address all cases where the comparison order check is violated
This commit is contained in:
Adam Turner
2022-12-30 00:06:11 +00:00
parent 2c97bfaa19
commit b91f6617ed
9 changed files with 119 additions and 113 deletions
-2
View File
@@ -160,8 +160,6 @@ ignore = [
'B905', 'B905',
# pygrep-hooks # pygrep-hooks
"PGH003", "PGH003",
# pylint
"PLC2201",
# flake8-bandit # flake8-bandit
"S101", # assert used "S101", # assert used
"S105", # possible hardcoded password "S105", # possible hardcoded password
+1 -1
View File
@@ -98,7 +98,7 @@ def prepend_prolog(content: StringList, prolog: str) -> None:
def append_epilog(content: StringList, epilog: str) -> None: def append_epilog(content: StringList, epilog: str) -> None:
"""Append a string to content body as epilog.""" """Append a string to content body as epilog."""
if epilog: if epilog:
if 0 < len(content): if len(content) > 0:
source, lineno = content.info(-1) source, lineno = content.info(-1)
else: else:
source = '<generated>' source = '<generated>'
+1 -1
View File
@@ -1085,7 +1085,7 @@ class LaTeXTranslator(SphinxTranslator):
def visit_enumerated_list(self, node: Element) -> None: def visit_enumerated_list(self, node: Element) -> None:
def get_enumtype(node: Element) -> str: def get_enumtype(node: Element) -> str:
enumtype = node.get('enumtype', 'arabic') enumtype = node.get('enumtype', 'arabic')
if 'alpha' in enumtype and 26 < node.get('start', 0) + len(node): if 'alpha' in enumtype and (node.get('start', 0) + len(node)) > 26:
# fallback to arabic if alphabet counter overflows # fallback to arabic if alphabet counter overflows
enumtype = 'arabic' enumtype = 'arabic'
+1 -1
View File
@@ -1623,7 +1623,7 @@ def test_latex_elements_extrapackages(app, status, warning):
@pytest.mark.sphinx('latex', testroot='nested-tables') @pytest.mark.sphinx('latex', testroot='nested-tables')
def test_latex_nested_tables(app, status, warning): def test_latex_nested_tables(app, status, warning):
app.builder.build_all() app.builder.build_all()
assert '' == warning.getvalue() assert warning.getvalue() == ''
@pytest.mark.sphinx('latex', testroot='latex-container') @pytest.mark.sphinx('latex', testroot='latex-container')
+2 -4
View File
@@ -87,10 +87,8 @@ def test_defaults_json(app):
'info': '404 Client Error: Not Found for url: https://www.google.com/image2.png' 'info': '404 Client Error: Not Found for url: https://www.google.com/image2.png'
} }
# looking for '#top' and '#does-not-exist' not found should fail # looking for '#top' and '#does-not-exist' not found should fail
assert "Anchor 'top' not found" == \ assert rowsby["https://www.google.com/#top"]["info"] == "Anchor 'top' not found"
rowsby["https://www.google.com/#top"]["info"] assert rowsby["http://www.sphinx-doc.org/en/master/index.html#does-not-exist"]["info"] == "Anchor 'does-not-exist' not found"
assert "Anchor 'does-not-exist' not found" == \
rowsby["http://www.sphinx-doc.org/en/master/index.html#does-not-exist"]["info"]
# images should fail # images should fail
assert "Not Found for url: https://www.google.com/image.png" in \ assert "Not Found for url: https://www.google.com/image.png" in \
rowsby["https://www.google.com/image.png"]["info"] rowsby["https://www.google.com/image.png"]["info"]
+4 -4
View File
@@ -504,13 +504,13 @@ def test_literalinclude_classes(app, status, warning):
code_block = secs[0].findall('literal_block') code_block = secs[0].findall('literal_block')
assert len(code_block) > 0 assert len(code_block) > 0
assert 'foo bar' == code_block[0].get('classes') assert code_block[0].get('classes') == 'foo bar'
assert 'code_block' == code_block[0].get('names') assert code_block[0].get('names') == 'code_block'
literalinclude = secs[1].findall('literal_block') literalinclude = secs[1].findall('literal_block')
assert len(literalinclude) > 0 assert len(literalinclude) > 0
assert 'bar baz' == literalinclude[0].get('classes') assert literalinclude[0].get('classes') == 'bar baz'
assert 'literal_include' == literalinclude[0].get('names') assert literalinclude[0].get('names') == 'literal_include'
@pytest.mark.sphinx('xml', testroot='directive-code') @pytest.mark.sphinx('xml', testroot='directive-code')
+106 -96
View File
@@ -1055,39 +1055,41 @@ def test_autodoc_typehints_description_no_undoc_doc_rtype(app):
) )
app.build() app.build()
context = (app.outdir / 'index.txt').read_text(encoding='utf8') context = (app.outdir / 'index.txt').read_text(encoding='utf8')
assert ('target.typehints.incr(a, b=1)\n' assert context == (
'\n' 'target.typehints.incr(a, b=1)\n'
' Return type:\n' '\n'
' int\n' ' Return type:\n'
'\n' ' int\n'
'target.typehints.decr(a, b=1)\n' '\n'
'\n' 'target.typehints.decr(a, b=1)\n'
' Returns:\n' '\n'
' decremented number\n' ' Returns:\n'
'\n' ' decremented number\n'
' Return type:\n' '\n'
' int\n' ' Return type:\n'
'\n' ' int\n'
'target.typehints.tuple_args(x)\n' '\n'
'\n' 'target.typehints.tuple_args(x)\n'
' Parameters:\n' '\n'
' **x** (*Tuple**[**int**, **Union**[**int**, **str**]**]*) -- arg\n' ' Parameters:\n'
'\n' ' **x** (*Tuple**[**int**, **Union**[**int**, **str**]**]*) -- arg\n'
' Returns:\n' '\n'
' another tuple\n' ' Returns:\n'
'\n' ' another tuple\n'
' Return type:\n' '\n'
' *Tuple*[int, int]\n' ' Return type:\n'
'\n' ' *Tuple*[int, int]\n'
'target.typehints.Math.nothing(self)\n' '\n'
'\n' 'target.typehints.Math.nothing(self)\n'
'target.typehints.Math.horse(self, a, b)\n' '\n'
'\n' 'target.typehints.Math.horse(self, a, b)\n'
' Returns:\n' '\n'
' nothing\n' ' Returns:\n'
'\n' ' nothing\n'
' Return type:\n' '\n'
' None\n' == context) ' Return type:\n'
' None\n'
)
@pytest.mark.sphinx('text', testroot='ext-autodoc', @pytest.mark.sphinx('text', testroot='ext-autodoc',
@@ -1100,30 +1102,32 @@ def test_autodoc_typehints_description_with_documented_init(app):
) )
app.build() app.build()
context = (app.outdir / 'index.txt').read_text(encoding='utf8') context = (app.outdir / 'index.txt').read_text(encoding='utf8')
assert ('class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n' assert context == (
'\n' 'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
' Class docstring.\n' '\n'
'\n' ' Class docstring.\n'
' Parameters:\n' '\n'
' * **x** (*int*) --\n' ' Parameters:\n'
'\n' ' * **x** (*int*) --\n'
' * **args** (*int*) --\n' '\n'
'\n' ' * **args** (*int*) --\n'
' * **kwargs** (*int*) --\n' '\n'
'\n' ' * **kwargs** (*int*) --\n'
' __init__(x, *args, **kwargs)\n' '\n'
'\n' ' __init__(x, *args, **kwargs)\n'
' Init docstring.\n' '\n'
'\n' ' Init docstring.\n'
' Parameters:\n' '\n'
' * **x** (*int*) -- Some integer\n' ' Parameters:\n'
'\n' ' * **x** (*int*) -- Some integer\n'
' * **args** (*int*) -- Some integer\n' '\n'
'\n' ' * **args** (*int*) -- Some integer\n'
' * **kwargs** (*int*) -- Some integer\n' '\n'
'\n' ' * **kwargs** (*int*) -- Some integer\n'
' Return type:\n' '\n'
' None\n' == context) ' Return type:\n'
' None\n'
)
@pytest.mark.sphinx('text', testroot='ext-autodoc', @pytest.mark.sphinx('text', testroot='ext-autodoc',
@@ -1137,20 +1141,22 @@ def test_autodoc_typehints_description_with_documented_init_no_undoc(app):
) )
app.build() app.build()
context = (app.outdir / 'index.txt').read_text(encoding='utf8') context = (app.outdir / 'index.txt').read_text(encoding='utf8')
assert ('class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n' assert context == (
'\n' 'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
' Class docstring.\n' '\n'
'\n' ' Class docstring.\n'
' __init__(x, *args, **kwargs)\n' '\n'
'\n' ' __init__(x, *args, **kwargs)\n'
' Init docstring.\n' '\n'
'\n' ' Init docstring.\n'
' Parameters:\n' '\n'
' * **x** (*int*) -- Some integer\n' ' Parameters:\n'
'\n' ' * **x** (*int*) -- Some integer\n'
' * **args** (*int*) -- Some integer\n' '\n'
'\n' ' * **args** (*int*) -- Some integer\n'
' * **kwargs** (*int*) -- Some integer\n' == context) '\n'
' * **kwargs** (*int*) -- Some integer\n'
)
@pytest.mark.sphinx('text', testroot='ext-autodoc', @pytest.mark.sphinx('text', testroot='ext-autodoc',
@@ -1167,20 +1173,22 @@ def test_autodoc_typehints_description_with_documented_init_no_undoc_doc_rtype(a
) )
app.build() app.build()
context = (app.outdir / 'index.txt').read_text(encoding='utf8') context = (app.outdir / 'index.txt').read_text(encoding='utf8')
assert ('class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n' assert context == (
'\n' 'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
' Class docstring.\n' '\n'
'\n' ' Class docstring.\n'
' __init__(x, *args, **kwargs)\n' '\n'
'\n' ' __init__(x, *args, **kwargs)\n'
' Init docstring.\n' '\n'
'\n' ' Init docstring.\n'
' Parameters:\n' '\n'
' * **x** (*int*) -- Some integer\n' ' Parameters:\n'
'\n' ' * **x** (*int*) -- Some integer\n'
' * **args** (*int*) -- Some integer\n' '\n'
'\n' ' * **args** (*int*) -- Some integer\n'
' * **kwargs** (*int*) -- Some integer\n' == context) '\n'
' * **kwargs** (*int*) -- Some integer\n'
)
@pytest.mark.sphinx('text', testroot='ext-autodoc', @pytest.mark.sphinx('text', testroot='ext-autodoc',
@@ -1383,17 +1391,19 @@ def test_autodoc_typehints_description_and_type_aliases(app):
(app.srcdir / 'autodoc_type_aliases.rst').write_text('.. autofunction:: target.autodoc_type_aliases.sum', encoding='utf8') (app.srcdir / 'autodoc_type_aliases.rst').write_text('.. autofunction:: target.autodoc_type_aliases.sum', encoding='utf8')
app.build() app.build()
context = (app.outdir / 'autodoc_type_aliases.txt').read_text(encoding='utf8') context = (app.outdir / 'autodoc_type_aliases.txt').read_text(encoding='utf8')
assert ('target.autodoc_type_aliases.sum(x, y)\n' assert context == (
'\n' 'target.autodoc_type_aliases.sum(x, y)\n'
' docstring\n' '\n'
'\n' ' docstring\n'
' Parameters:\n' '\n'
' * **x** (*myint*) --\n' ' Parameters:\n'
'\n' ' * **x** (*myint*) --\n'
' * **y** (*myint*) --\n' '\n'
'\n' ' * **y** (*myint*) --\n'
' Return type:\n' '\n'
' myint\n' == context) ' Return type:\n'
' myint\n'
)
@pytest.mark.sphinx('html', testroot='ext-autodoc', @pytest.mark.sphinx('html', testroot='ext-autodoc',
+1 -1
View File
@@ -415,7 +415,7 @@ def test_load_mappings_fallback(tempdir, app, status, warning):
normalize_intersphinx_mapping(app, app.config) normalize_intersphinx_mapping(app, app.config)
load_mappings(app) load_mappings(app)
assert "encountered some issues with some of the inventories" in status.getvalue() assert "encountered some issues with some of the inventories" in status.getvalue()
assert "" == warning.getvalue() assert warning.getvalue() == ""
rn = reference_check(app, 'py', 'func', 'module1.func', 'foo') rn = reference_check(app, 'py', 'func', 'module1.func', 'foo')
assert isinstance(rn, nodes.reference) assert isinstance(rn, nodes.reference)
+3 -3
View File
@@ -168,14 +168,14 @@ def test_extract_messages_without_rawsource():
def test_clean_astext(): def test_clean_astext():
node = nodes.paragraph(text='hello world') node = nodes.paragraph(text='hello world')
assert 'hello world' == clean_astext(node) assert clean_astext(node) == 'hello world'
node = nodes.image(alt='hello world') node = nodes.image(alt='hello world')
assert '' == clean_astext(node) assert clean_astext(node) == ''
node = nodes.paragraph(text='hello world') node = nodes.paragraph(text='hello world')
node += nodes.raw('', 'raw text', format='html') node += nodes.raw('', 'raw text', format='html')
assert 'hello world' == clean_astext(node) assert clean_astext(node) == 'hello world'
@pytest.mark.parametrize( @pytest.mark.parametrize(