diff --git a/bundled/tool/lsp_server.py b/bundled/tool/lsp_server.py index 24635aa..49eb25b 100644 --- a/bundled/tool/lsp_server.py +++ b/bundled/tool/lsp_server.py @@ -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')" + ), + } + ], } diff --git a/pyproject.toml b/pyproject.toml index 6040ba9..f042172 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,7 +47,6 @@ disable= [ "too-many-positional-arguments", "too-many-branches", "too-many-instance-attributes", - "unspecified-encoding", "wrong-import-position", ] diff --git a/src/test/python_tests/test_code_actions.py b/src/test/python_tests/test_code_actions.py index 62baa19..7a18413 100644 --- a/src/test/python_tests/test_code_actions.py +++ b/src/test/python_tests/test_code_actions.py @@ -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: """, ), ],