Python extension pack
This pack of visual studio code extensions allows you to automate good practices for developing and writing code in Python through the correct configuration of the tooling.
Content
Config files
For the correct functioning of the extensions it is necessary:
- Create a pyproject.toml file in the root of the project and paste the following configuration into it:
[tool.mypy]
# Platform configuration
python_version = "3.12"
# imports related
ignore_missing_imports = true
follow_imports = "silent"
# None and Optional handling
no_implicit_optional = true
strict_optional = true
# Configuring warnings
warn_unused_configs = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_unreachable = true
warn_return_any = false
# Untyped definitions and calls
check_untyped_defs = true
disallow_untyped_calls = false
disallow_untyped_defs = true
disallow_incomplete_defs = true
disallow_untyped_decorators = false
# Disallow dynamic typing
disallow_subclassing_any = true
disallow_any_unimported = false
disallow_any_expr = false
disallow_any_decorated = false
disallow_any_explicit = false
disallow_any_generics = false
# Miscellaneous strictness flags
allow_untyped_globals = true
allow_redefinition = false
local_partial_types = false
implicit_reexport = true
strict_equality = true
# Configuring error messages
show_error_context = false
show_column_numbers = false
show_error_codes = true
[tool.ruff]
target-version = "py312"
line-length = 80
extend-exclude = ["tests", "test"]
[tool.ruff.lint]
select = ["F", "E", "D"]
extend-select = ["W", "N", "UP", "B", "A", "C4", "PT", "SIM", "PD", "PLE", "RUF"]
ignore = ["SIM300"]
fixable = ["F", "I", "E", "W", "UP", "B", "A", "C4"]
unfixable = []
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
[tool.ruff.lint.isort]
no-sections = false
force-single-line = true
force-sort-within-sections = false
lines-after-imports = 2
section-order = [
"future",
"standard-library",
"third-party",
"first-party",
"local-folder"
]
default-section = "first-party"
known-third-party = [
"numpy",
"pandas",
"keras",
"tensorflow",
"sklearn",
"matplotlib",
"scipy",
"h5py",
"seaborn",
"numba",
"gym",
"PyQt6",
"PyQt5",
"pyqtgraph",
"torch",
"tqdm"
]
[tool.ruff.lint.mccabe]
max-complexity = 10
[tool.ruff.lint.pycodestyle]
ignore-overlong-task-comments = true
[tool.ruff.lint.pydocstyle]
convention = "numpy"
[tool.ruff.lint.flake8-annotations]
allow-star-arg-any = false
ignore-fully-untyped = false
- Create a settings.json file inside the .vscode folder with the following settings:
{
"python.languageServer": "Pylance",
// Ruff config.
"[python]": {
"editor.detectIndentation": false,
"editor.insertSpaces": true,
"editor.tabSize": 4,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
"editor.formatOnSave": true
},
"notebook.formatOnSave.enabled": true,
"notebook.codeActionsOnSave": {
"notebook.source.fixAll": "explicit",
"notebook.source.organizeImports": "explicit"
},
"ruff.importStrategy": "fromEnvironment",
"ruff.lint.args": ["--config=pyproject.toml"],
// MyPy config.
"mypy-type-checker.args": ["--config-file=pyproject.toml"],
"mypy-type-checker.importStrategy": "fromEnvironment",
// autoDocstring config.
"autoDocstring.docstringFormat": "numpy",
"autoDocstring.includeName": true,
"autoDocstring.startOnNewLine": false,
"autoDocstring.generateDocstringOnEnter": true,
// Files config.
"files.trimTrailingWhitespace": true,
"files.trimFinalNewlines": true,
// Editor config.
"editor.renderWhitespace": "boundary",
"editor.formatOnSave": true,
"pythonIndent.trimLinesWithOnlyWhitespace": true
}
- Create a Python virtual environment and install the following development libraries:
mypy>=1.10.0
ruff>=0.0.270
Enjoy!