Skip to content

Commit cf160d8

Browse files
authored
Merge pull request #15 from bryanforbes/handle-pyi-files
Correctly handle .pyi files
2 parents 79b6884 + 531e95b commit cf160d8

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

pyls_black/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def load_config(filename: str) -> Dict:
7070
"line_length": 88,
7171
"fast": False,
7272
"py36": False,
73-
"pyi": False,
73+
"pyi": filename.endswith(".pyi"),
7474
"skip_string_normalization": False,
7575
"skip_numeric_underscore_normalization": False,
7676
}

pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
[tool.black]
22
py36 = true
3+
exclude = '''
4+
/(
5+
\.venv
6+
| \.git
7+
| \.mypy_cache
8+
| build
9+
| dist
10+
| fixtures
11+
)/
12+
'''

tests/fixtures/formatted.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def foo() -> None: ...
2+
def bar() -> int: ...

tests/fixtures/unformatted.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def foo() -> None:
2+
...
3+
4+
def bar() -> int:
5+
...

tests/test_plugin.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,27 @@ def unformatted_document():
1616
return Document(uri)
1717

1818

19+
@pytest.fixture
20+
def unformatted_pyi_document():
21+
path = fixtures_dir / "unformatted.pyi"
22+
uri = f"file:/{path}"
23+
return Document(uri)
24+
25+
1926
@pytest.fixture
2027
def formatted_document():
2128
path = fixtures_dir / "formatted.txt"
2229
uri = f"file:/{path}"
2330
return Document(uri)
2431

2532

33+
@pytest.fixture
34+
def formatted_pyi_document():
35+
path = fixtures_dir / "formatted.pyi"
36+
uri = f"file:/{path}"
37+
return Document(uri)
38+
39+
2640
@pytest.fixture
2741
def invalid_document():
2842
path = fixtures_dir / "invalid.txt"
@@ -51,12 +65,32 @@ def test_pyls_format_document(unformatted_document, formatted_document):
5165
]
5266

5367

68+
def test_pyls_format_pyi_document(unformatted_pyi_document, formatted_pyi_document):
69+
result = pyls_format_document(unformatted_pyi_document)
70+
71+
assert result == [
72+
{
73+
"range": {
74+
"start": {"line": 0, "character": 0},
75+
"end": {"line": 5, "character": 0},
76+
},
77+
"newText": formatted_pyi_document.source,
78+
}
79+
]
80+
81+
5482
def test_pyls_format_document_unchanged(formatted_document):
5583
result = pyls_format_document(formatted_document)
5684

5785
assert result == []
5886

5987

88+
def test_pyls_format_pyi_document_unchanged(formatted_pyi_document):
89+
result = pyls_format_document(formatted_pyi_document)
90+
91+
assert result == []
92+
93+
6094
def test_pyls_format_document_syntax_error(invalid_document):
6195
result = pyls_format_document(invalid_document)
6296

@@ -125,7 +159,7 @@ def test_pyls_format_range_syntax_error(invalid_document):
125159

126160

127161
def test_load_config():
128-
config = load_config(fixtures_dir / "config" / "example.py")
162+
config = load_config(str(fixtures_dir / "config" / "example.py"))
129163

130164
assert config == {
131165
"line_length": 20,
@@ -138,7 +172,7 @@ def test_load_config():
138172

139173

140174
def test_load_config_defaults():
141-
config = load_config(fixtures_dir / "example.py")
175+
config = load_config(str(fixtures_dir / "example.py"))
142176

143177
assert config == {
144178
"line_length": 88,

0 commit comments

Comments
 (0)