Skip to content

Commit

Permalink
chore: Run black and ignore incompatible flake8 codes
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrelg committed Jul 16, 2023
1 parent c773540 commit 699f80b
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 368 deletions.
9 changes: 9 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ ignore=
D107,
# W503 line break before binary operator
W503,
# New black update seem to be incompatible with the next 3
# E231 missing whitespace after ':'
# E241 multiple spaces after ':'
# E272 multiple spaces before keyword
# E271 multiple spaces after keyword
E231,
E241,
E272,
E271,
exclude =
.git,
.venv
Expand Down
30 changes: 10 additions & 20 deletions tests/test_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def test_attrs_model(imp, dec):
Test `attrs` classes together with a non-`attrs` class that has a class var of the same type.
`attrs` classes are instantiated using different dataclass decorators. The `attrs` module is imported as whole.
"""
example = textwrap.dedent(
f'''
example = textwrap.dedent(f'''
{imp}
from decimal import Decimal
Expand All @@ -43,8 +42,7 @@ class Y:
class Z:
x: Decimal
'''
)
''')
assert _get_error(example, error_code_filter='TC001,TC002,TC003') == set()


Expand All @@ -65,8 +63,7 @@ def test_complex_attrs_model(imp, dec, expected):
Test `attrs` classes together with a non-`attrs` class tha has a class var of another type.
`attrs` classes are instantiated using different dataclass decorators. The `attrs` module is imported as whole.
"""
example = textwrap.dedent(
f'''
example = textwrap.dedent(f'''
{imp}
from decimals import Decimal
from decimal import Context
Expand All @@ -81,8 +78,7 @@ class Y:
class Z:
x: Context
'''
)
''')
assert _get_error(example, error_code_filter='TC001,TC002,TC003') == expected


Expand All @@ -103,8 +99,7 @@ def test_complex_attrs_model_direct_import(imp, dec, expected):
Test `attrs` classes together with a non-`attrs` class tha has a class var of another type.
`attrs` classes are instantiated using different dataclass decorators which are imported as submodules.
"""
example = textwrap.dedent(
f'''
example = textwrap.dedent(f'''
{imp}
from decimals import Decimal
from decimal import Context
Expand All @@ -119,8 +114,7 @@ class Y:
class Z:
x: Context
'''
)
''')
assert _get_error(example, error_code_filter='TC001,TC002,TC003') == expected


Expand Down Expand Up @@ -150,8 +144,7 @@ def test_complex_attrs_model_as_import(imp, dec, expected):
`attrs` classes are instantiated using different dataclass
decorators which are imported as submodules using an alias.
"""
example = textwrap.dedent(
f'''
example = textwrap.dedent(f'''
{imp}
from decimals import Decimal
from decimal import Context
Expand All @@ -166,8 +159,7 @@ class Y:
class Z:
x: Context
'''
)
''')
assert _get_error(example, error_code_filter='TC001,TC002,TC003') == expected


Expand All @@ -193,8 +185,7 @@ def test_complex_attrs_model_slots_frozen(imp, dec, expected):
Test `attrs` classes together with a non-`attrs` class tha has a class var of another type.
`attrs` classes are instantiated using different dataclass decorators and arguments.
"""
example = textwrap.dedent(
f'''
example = textwrap.dedent(f'''
{imp}
from decimals import Decimal
from decimal import Context
Expand All @@ -209,6 +200,5 @@ class Y:
class Z:
x: Context
'''
)
''')
assert _get_error(example, error_code_filter='TC001,TC002,TC003') == expected
68 changes: 22 additions & 46 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@

class TestFoundBugs:
def test_mixed_errors(self):
example = textwrap.dedent(
f"""
example = textwrap.dedent(f"""
import {mod}
import pytest
from x import y
x: {mod} | pytest | y
"""
)
""")
assert _get_error(example) == {
f"2:0 {TC001.format(module=f'{mod}')}",
'3:0 ' + TC002.format(module='pytest'),
'4:0 ' + TC002.format(module='x.y'),
}

def test_type_checking_block_imports_dont_generate_errors(self):
example = textwrap.dedent(
"""
example = textwrap.dedent("""
import x
from y import z
Expand All @@ -39,8 +36,7 @@ def test_type_checking_block_imports_dont_generate_errors(self):
def test(foo: z, bar: x):
pass
"""
)
""")
assert _get_error(example) == {
'2:0 ' + TC002.format(module='x'),
'3:0 ' + TC002.format(module='y.z'),
Expand All @@ -51,8 +47,7 @@ def test_model_declarations_dont_trigger_error(self):
Initially found false positives in Django project, because name
visitor did not capture the SomeModel usage in the example below.
"""
example = textwrap.dedent(
"""
example = textwrap.dedent("""
from django.db import models
from app.models import SomeModel
Expand All @@ -61,76 +56,64 @@ class LoanProvider(models.Model):
SomeModel,
on_delete=models.CASCADE,
)
"""
)
""")
assert _get_error(example) == set()

def test_all_list_declaration(self):
"""__all__ declarations originally generated false positives."""
example = textwrap.dedent(
"""
example = textwrap.dedent("""
from app.models import SomeModel
from another_app.models import AnotherModel
__all__ = [
'SomeModel',
'AnotherModel'
]
"""
)
""")
assert _get_error(example) == set()

def test_all_tuple_declaration(self):
"""__all__ declarations originally generated false positives."""
example = textwrap.dedent(
"""
example = textwrap.dedent("""
from app.models import SomeModel
from another_app.models import AnotherModel
__all__ = (
'SomeModel',
'AnotherModel'
)
"""
)
""")
assert _get_error(example) == set()

def test_callable_import(self):
"""__all__ declarations originally generated false positives."""
example = textwrap.dedent(
"""
example = textwrap.dedent("""
from x import y
class X:
def __init__(self):
self.all_sellable_models: list[CostModel] = y(
country=self.country
)
"""
)
""")
assert _get_error(example) == set()

def test_ellipsis(self):
example = textwrap.dedent(
"""
example = textwrap.dedent("""
x: Tuple[str, ...]
"""
)
""")
assert _get_error(example) == set()

def test_literal(self):
example = textwrap.dedent(
"""
example = textwrap.dedent("""
from __future__ import annotations
x: Literal['string']
"""
)
""")
assert _get_error(example) == set()

def test_conditional_import(self):
example = textwrap.dedent(
"""
example = textwrap.dedent("""
version = 2
if version == 2:
Expand All @@ -139,8 +122,7 @@ def test_conditional_import(self):
import y as x
var: x
"""
)
""")
assert _get_error(example) == {"7:4 TC002 Move third-party import 'x' into a type-checking block"}

def test_type_checking_block_formats_detected(self):
Expand Down Expand Up @@ -326,17 +308,15 @@ def test_tc004_false_positive(self, example):

def test_tc002_false_positive(self):
"""Re https://github.com/snok/flake8-type-checking/issues/120."""
example = textwrap.dedent(
"""
example = textwrap.dedent("""
from logging import INFO
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR
class C:
level: int = INFO
status: int = HTTP_500_INTERNAL_SERVER_ERROR
"""
)
""")
assert _get_error(example) == set()

def test_tc001_false_positive(self):
Expand All @@ -345,15 +325,11 @@ def test_tc001_false_positive(self):

def test_works_with_other_plugins(self, flake8_path):
"""Re https://github.com/snok/flake8-type-checking/issues/139."""
(flake8_path / 'example.py').write_text(
textwrap.dedent(
'''
(flake8_path / 'example.py').write_text(textwrap.dedent('''
import os
t = os.path.dirname(os.path.realpath(__file__))
'''
)
)
'''))
result = flake8_path.run_flake8()
assert result.out_lines == [
'./example.py:4:5: PL120 os.path.dirname("foo/bar") should be replaced by bar_path.parent',
Expand Down
24 changes: 8 additions & 16 deletions tests/test_exempt_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@ def test_exempt_modules_option():
which is meant to passlist certain modules from TC001 and TC002 errors.
"""
# Check that typing is passlisted when exempted
example = textwrap.dedent(
'''
example = textwrap.dedent('''
from typing import TYPE_CHECKING
from pandas import DataFrame
x: DataFrame
'''
)
''')
assert _get_error(example, error_code_filter='TC002') == {'3:0 ' + TC002.format(module='pandas.DataFrame')}
assert _get_error(example, error_code_filter='TC002', type_checking_exempt_modules=['pandas']) == set()

# Check that other basic errors are still caught
example2 = textwrap.dedent(
'''
example2 = textwrap.dedent('''
from typing import TYPE_CHECKING
from pandas import DataFrame
from a import B
x: Callable[[DataFrame, B], List]
'''
)
''')
assert _get_error(example2, error_code_filter='TC002') == {
'3:0 ' + TC002.format(module='pandas.DataFrame'),
'4:0 ' + TC002.format(module='a.B'),
Expand All @@ -40,26 +36,22 @@ def test_exempt_modules_option():
}

# Check Import
example3 = textwrap.dedent(
'''
example3 = textwrap.dedent('''
import pandas
x: pandas.DataFrame
'''
)
''')
assert _get_error(example3, error_code_filter='TC002') == {'2:0 ' + TC002.format(module='pandas')}
assert _get_error(example3, error_code_filter='TC002', type_checking_exempt_modules=['pandas']) == set()

# Check template Import
example4 = textwrap.dedent(
'''
example4 = textwrap.dedent('''
from apps.app_1.choices import ExampleChoice
from apps.app_2.choices import Example2Choice
x: ExampleChoice
y: Example2Choice
'''
)
''')
assert _get_error(example4, error_code_filter='TC002') == {
'2:0 ' + TC002.format(module='apps.app_1.choices.ExampleChoice'),
'3:0 ' + TC002.format(module='apps.app_2.choices.Example2Choice'),
Expand Down

0 comments on commit 699f80b

Please sign in to comment.