Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff instead of black #16161

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 3 additions & 4 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,10 @@ jobs:
with:
python-version: ${{env.PYTHON_VERSION}}

- name: Run Black on Python code
- name: Run Ruff on Python code
run: |
python -m pip install click==8.0.4
python -m pip install -U black
python -m black . --check
python -m pip install -U ruff
python -m ruff check ./pythonFiles/*.py
working-directory: pythonFiles

- name: Run gulp prePublishNonBundle
Expand Down
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"editorconfig.editorconfig",
"esbenp.prettier-vscode",
"hbenl.vscode-mocha-test-adapter",
"ms-python.black",
"charliermarsh.ruff",
"ms-vscode.extension-test-runner"
]
}
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
- [EditorConfig for VS Code](https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig)
- [Python Extension for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-python.python)
- [Ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff)

### Setup

Expand All @@ -37,7 +38,7 @@ python3 -m venv .venv
source .venv/bin/activate
# and in Windows cmd or PowerShell
.venv\Scripts\activate
# The Python code in the extension is formatted using Black.
# The Python code in the extension is formatted using Ruff.
python -m pip install black
```

Expand Down
1 change: 0 additions & 1 deletion build/existingFiles.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@
"src/extension.ts",
"src/platform/formatters/autoPep8Formatter.ts",
"src/platform/formatters/baseFormatter.ts",
"src/platform/formatters/blackFormatter.ts",
"src/platform/formatters/dummyFormatter.ts",
"src/platform/formatters/helper.ts",
"src/platform/formatters/lineFormatter.ts",
Expand Down
1 change: 0 additions & 1 deletion build/unlocalizedFiles.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"src/platform/common/installer/productInstaller.ts",
"src/platform/debugger/extension/hooks/childProcessAttachService.ts",
"src/platform/formatters/baseFormatter.ts",
"src/platform/formatters/blackFormatter.ts",
"src/platform/interpreter/configuration/pythonPathUpdaterService.ts",
"src/platform/linters/errorHandlers/notInstalled.ts",
"src/platform/linters/errorHandlers/standard.ts",
Expand Down
10 changes: 9 additions & 1 deletion pythonFiles/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@
"**/__pycache__/**": true,
"**/**/*.pyc": true
},
"python.formatting.provider": "black"
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
},
"notebook.codeActionsOnSave": {
"notebook.source.fixAll": "explicit"
}
}
}
6 changes: 5 additions & 1 deletion pythonFiles/tests/ipython/getJupyterVariableList.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


# Query Jupyter server for defined variables list
# Tested on 2.7 and 3.6
from sys import getsizeof as _VSCODE_getsizeof
Expand Down Expand Up @@ -35,7 +39,7 @@
)
del _VSCode_type
del _VSCode_var
except:
except: # noqa: E722
pass

builtins.print(_VSCODE_json.dumps(_VSCode_output))
Expand Down
38 changes: 21 additions & 17 deletions pythonFiles/tests/ipython/getJupyterVariableValue.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


import sys as VC_sys
import locale as VC_locale

Expand All @@ -9,11 +13,11 @@
class VC_SafeRepr(object):
# Py3 compat - alias unicode to str, and xrange to range
try:
unicode # noqa
unicode # type: ignore # noqa
except NameError:
unicode = str
try:
xrange # noqa
xrange # type: ignore # noqa
except NameError:
xrange = range

Expand All @@ -38,7 +42,7 @@ class VC_SafeRepr(object):
string_types = (str, unicode)
set_info = (set, "set([", "])", False)
frozenset_info = (frozenset, "frozenset([", "])", False)
int_types = (int, long) # noqa
int_types = (int, long) # type: ignore # noqa
long_iter_types = (
list,
tuple,
Expand All @@ -47,7 +51,7 @@ class VC_SafeRepr(object):
dict,
set,
frozenset,
buffer,
buffer, # type: ignore # noqa: F821
) # noqa

# Collection types are recursively iterated for each limit in
Expand Down Expand Up @@ -90,9 +94,9 @@ class VC_SafeRepr(object):

def __call__(self, obj):
try:
if VC_IS_PY2:
if VC_IS_PY2: # noqa: F821
return "".join(
(x.encode("utf-8") if isinstance(x, unicode) else x)
(x.encode("utf-8") if isinstance(x, unicode) else x) # noqa: F821 # type: ignore
for x in self._repr(obj, 0)
)
else:
Expand Down Expand Up @@ -172,7 +176,7 @@ def _is_long_iter(self, obj, level=0):
return False

# xrange reprs fine regardless of length.
if isinstance(obj, xrange):
if isinstance(obj, xrange): # type: ignore # noqa: F821
return False

# numpy and scipy collections (ndarray etc) have
Expand Down Expand Up @@ -345,17 +349,17 @@ def _repr_obj(self, obj, level, limit_inner, limit_outer):
max(1, int(limit / 3)),
) # noqa

if VC_IS_PY2 and isinstance(obj_repr, bytes):
if VC_IS_PY2 and isinstance(obj_repr, bytes): # noqa: F821
# If we can convert to unicode before slicing, that's better (but don't do
# it if it's not possible as we may be dealing with actual binary data).

obj_repr = self._bytes_as_unicode_if_possible(obj_repr)
if isinstance(obj_repr, unicode):
if isinstance(obj_repr, unicode): # type: ignore # noqa: F821
# Deal with high-surrogate leftovers on Python 2.
try:
if left_count > 0 and unichr(0xD800) <= obj_repr[
if left_count > 0 and unichr(0xD800) <= obj_repr[ # noqa: F821 # type: ignore
left_count - 1
] <= unichr(0xDBFF):
] <= unichr(0xDBFF): # type: ignore # noqa: F821
left_count -= 1
except ValueError:
# On Jython unichr(0xD800) will throw an error:
Expand All @@ -371,9 +375,9 @@ def _repr_obj(self, obj, level, limit_inner, limit_outer):

# Deal with high-surrogate leftovers on Python 2.
try:
if right_count > 0 and unichr(0xD800) <= obj_repr[
if right_count > 0 and unichr(0xD800) <= obj_repr[ # noqa: F821 # type: ignore
-right_count - 1
] <= unichr(0xDBFF):
] <= unichr(0xDBFF): # type: ignore # noqa: F821
right_count -= 1
except ValueError:
# On Jython unichr(0xD800) will throw an error:
Expand All @@ -392,7 +396,7 @@ def _repr_obj(self, obj, level, limit_inner, limit_outer):
yield obj_repr[-right_count:]

def _convert_to_unicode_or_bytes_repr(self, obj_repr):
if VC_IS_PY2 and isinstance(obj_repr, bytes):
if VC_IS_PY2 and isinstance(obj_repr, bytes): # noqa: F821
obj_repr = self._bytes_as_unicode_if_possible(obj_repr)
if isinstance(obj_repr, bytes):
# If we haven't been able to decode it this means it's some binary data
Expand All @@ -406,12 +410,12 @@ def _bytes_as_unicode_if_possible(self, obj_repr):
# locale.getpreferredencoding() and 'utf-8). If no encoding can decode
# the input, we return the original bytes.
try_encodings = []
encoding = self.sys_stdout_encoding or getattr(VC_sys.stdout, "encoding", "")
encoding = self.sys_stdout_encoding or getattr(VC_sys.stdout, "encoding", "") # noqa: F821
if encoding:
try_encodings.append(encoding.lower())

preferred_encoding = (
self.locale_preferred_encoding or VC_locale.getpreferredencoding()
self.locale_preferred_encoding or VC_locale.getpreferredencoding() # noqa: F821
)
if preferred_encoding:
preferred_encoding = preferred_encoding.lower()
Expand All @@ -431,7 +435,7 @@ def _bytes_as_unicode_if_possible(self, obj_repr):


# Query Jupyter server for the value of a variable
import json as _VSCODE_json
import json as _VSCODE_json # noqa: E402

_VSCODE_max_len = 200
# In IJupyterVariables.getValue this '_VSCode_JupyterTestValue' will be replaced with the json stringified value of the target variable
Expand Down
6 changes: 3 additions & 3 deletions pythonFiles/tests/ipython/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def check_for_ipython():
try:
from IPython import get_ipython

return not get_ipython() == None
return not get_ipython() == None # noqa: E711
except ImportError:
pass
return False
Expand All @@ -33,7 +33,7 @@ def execute_script(file, replace_dict=dict([])):
# Replace the key value pairs
contents += (
line
if regex == None
if regex is None
else regex.sub(lambda m: replace_dict[m.group()], line)
)

Expand All @@ -44,7 +44,7 @@ def execute_script(file, replace_dict=dict([])):

def execute_code(code):
# Execute this script as a cell
result = get_ipython().run_cell(code)
result = get_ipython().run_cell(code) # type: ignore # noqa: F821
return result


Expand Down
2 changes: 1 addition & 1 deletion pythonFiles/tests/ipython/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_dataframe_info(capsys):
df = get_variable_value(vars, "df", capsys)
se = get_variable_value(vars, "se", capsys)
np = get_variable_value(vars, "np1", capsys)
np2 = get_variable_value(vars, "np2", capsys)
np2 = get_variable_value(vars, "np2", capsys) # noqa: F841
ls = get_variable_value(vars, "ls", capsys)
obj = get_variable_value(vars, "obj", capsys)
df3 = get_variable_value(vars, "df3", capsys)
Expand Down
2 changes: 1 addition & 1 deletion pythonFiles/tests/run_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

sys.path[0] = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

from tests.__main__ import main, parse_args
from tests.__main__ import main, parse_args # noqa: E402


if __name__ == "__main__":
Expand Down
2 changes: 0 additions & 2 deletions pythonFiles/tests/test_normalize_selection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import pytest
import sys
import textwrap

import normalizeSelection
Expand Down
Loading
Loading