Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,18 @@ def organize_imports(
"repl": r"for \1, \2 in \3.items():",
}
],
"W1514:unspecified-encoding": [
{
"pattern": re.compile(
r"open\(\s*([^,)\s]+)\s*(?:,\s*['\"]([^'\"]+)['\"])?\s*\)"
),
"repl": lambda m: (
f"open({m.group(1)}, '{m.group(2)}', encoding='utf-8')"
if m.group(2)
else f"open({m.group(1)}, encoding='utf-8')"
),
}
],
}


Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ disable= [
"too-many-positional-arguments",
"too-many-branches",
"too-many-instance-attributes",
"unspecified-encoding",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this removal, tests fail with

>               assert_that(len(diagnostics), is_(greater_than(0)))
E               AssertionError: 
E               Expected: a value greater than <0>
E                    but: was <0>

src/test/python_tests/test_code_actions.py:406: AssertionError
------------------------------------------------------------------------------------------ Captured log call ------------------------------------------------------------------------------------------
ERROR    concurrent.futures:_base.py:344 exception calling callback for <Future at 0x729661915600 state=finished returned NoneType>

"wrong-import-position",
]

Expand Down
108 changes: 108 additions & 0 deletions src/test/python_tests/test_code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,114 @@ class Banana(object):
print(f"{city} has population {population}.")
""",
"""for city, population in data.items():
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt') as f:
content = f.read()
""",
"""with open('file.txt', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'r') as f:
content = f.read()
""",
"""with open('file.txt', 'r', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'w') as f:
f.write('Hello, world!')
""",
"""with open('file.txt', 'w', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'a') as f:
f.write('Hello, world!')
""",
"""with open('file.txt', 'a', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'x') as f:
f.write('Hello, world!')
""",
"""with open('file.txt', 'x', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'x+') as f:
f.write('Hello, world!')
""",
"""with open('file.txt', 'x+', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'rt') as f:
content = f.read()
""",
"""with open('file.txt', 'rt', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'wt') as f:
f.write('Hello, world!')
""",
"""with open('file.txt', 'wt', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'at') as f:
f.write('Hello, world!')
""",
"""with open('file.txt', 'at', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'rt+') as f:
content = f.read()
""",
"""with open('file.txt', 'rt+', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'wt+') as f:
f.write('Hello, world!')
""",
"""with open('file.txt', 'wt+', encoding='utf-8') as f:
""",
),
(
"W1514:unspecified-encoding",
"""
with open('file.txt', 'at+') as f:
f.write('Hello, world!')
""",
"""with open('file.txt', 'at+', encoding='utf-8') as f:
""",
),
],
Expand Down
Loading