| name | code-style |
| description | Guide for running code style linters and formatters. Use this when asked to check code style, run linters, or fix formatting issues. |
Code Style and Linting
This skill covers code style checking and formatting workflows for the Semantic Link Labs project.
When to Use This Skill
Use this skill when you need to:
- Check code for style violations
- Format code with black
- Verify type correctness with mypy
- Fix linting errors before committing
- Ensure code follows project standards
Formatting Tools
| Tool | Purpose | Configuration |
|---|
| black | Code formatting | pyproject.toml |
| flake8 | Style checking | pyproject.toml |
| mypy | Type checking | pyproject.toml |
Code Formatting with Black
The project uses black for code formatting.
Running Black
black src/sempy_labs tests
black --check src/sempy_labs tests
black --diff src/sempy_labs tests
black src/sempy_labs/_workspaces.py
Black Configuration
From pyproject.toml, max line length is configured via flake8 settings.
Style Checking with Flake8
Running Flake8
flake8 src/sempy_labs tests
flake8 src/sempy_labs/_helper_functions.py
flake8 --show-source src/sempy_labs
flake8 --statistics src/sempy_labs
Flake8 Configuration
From pyproject.toml:
[tool.flake8]
max-line-length = 200
Common Flake8 Errors
| Code | Description | Fix |
|---|
| E501 | Line too long (>200 chars) | Break line or shorten code |
| F401 | Imported but unused | Remove unused import |
| F841 | Variable assigned but never used | Remove or use the variable |
| E302 | Expected 2 blank lines | Add blank lines between functions |
| E303 | Too many blank lines | Remove extra blank lines |
| W291 | Trailing whitespace | Remove trailing spaces |
| E711 | Comparison to None | Use is None instead of == None |
Type Checking with Mypy
Running Mypy
mypy src/sempy_labs
mypy src/sempy_labs/_workspaces.py
mypy --show-error-codes src/sempy_labs
Mypy Configuration
From pyproject.toml:
[[tool.mypy.overrides]]
module = "sempy.*,Microsoft.*,System.*,anytree.*,synapse.ml.services.*,polib.*,jsonpath_ng.*,anywidget.*,sqlglot.*"
ignore_missing_imports = true
Common Mypy Errors
| Error | Description | Fix |
|---|
Incompatible types | Type mismatch | Fix type annotation or value |
Missing return statement | Function missing return | Add return statement |
Argument has incompatible type | Wrong argument type | Pass correct type |
has no attribute | Attribute doesn't exist | Check spelling or type |
Adding Type Ignore Comments
When mypy raises false positives:
result = some_function()
result = some_function()
Import Organization
Import Order
Organize imports in this order (with blank lines between groups):
- Standard library imports
- Third-party imports
- Local application imports
Example Import Structure
import os
import json
from typing import Optional, List
from uuid import UUID
import pandas as pd
import requests
import sempy.fabric as fabric
from sempy._utils._log import log
import sempy_labs._icons as icons
from sempy_labs._helper_functions import (
resolve_workspace_id,
_base_api,
)
Pre-Commit Style Checklist
Before committing code changes:
black src/sempy_labs tests
flake8 src/sempy_labs tests
mypy src/sempy_labs
Common Style Fixes
Line Too Long
Break long lines using parentheses or backslash:
result = some_very_long_function_call(parameter1, parameter2, parameter3, parameter4, parameter5)
result = some_very_long_function_call(
parameter1,
parameter2,
parameter3,
parameter4,
parameter5
)
F-String Line Breaks
print(f"{icons.green_dot} The '{item_name}' {item_type} has been successfully created in the '{workspace_name}' workspace.")
print(
f"{icons.green_dot} The '{item_name}' {item_type} has been successfully "
f"created in the '{workspace_name}' workspace."
)
Dictionary Formatting
payload = {"displayName": name, "description": description, "definition": definition}
payload = {
"displayName": name,
"description": description,
"definition": definition,
}
IDE Integration
VS Code Settings
Recommended .vscode/settings.json:
{
"python.formatting.provider": "black",
"python.linting.flake8Enabled": true,
"python.linting.mypyEnabled": true,
"editor.formatOnSave": true,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}