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

Fiddle: update mypy rules for pydantic + move all config to pyproject.toml #24

Merged
merged 3 commits into from
Aug 22, 2024
Merged
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
1 change: 0 additions & 1 deletion buildpy/bandit.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion buildpy/lint.py → buildpy/lint_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_messages(self) -> List[Message]:
@click.option('-c', '--config-file-path', 'configFilePath', required=False, type=str)
def run(targets: List[str], outputFilename: str, outputFormat: str, configFilePath: str) -> None:
currentDirectory = os.path.dirname(os.path.realpath(__file__))
pylintConfigFilePath = configFilePath or f'{currentDirectory}/pylintrc'
pylintConfigFilePath = configFilePath or f'{currentDirectory}/pyproject.toml'
pylintMessageParser = PylintMessageParser()
run_pylint([f'--rcfile={pylintConfigFilePath}', f'--jobs=0'] + list(targets), reporter=pylintMessageParser, exit=False)
reporter = GitHubAnnotationsReporter() if outputFormat == 'annotations' else PrettyReporter()
Expand Down
41 changes: 0 additions & 41 deletions buildpy/mypy.ini

This file was deleted.

69 changes: 0 additions & 69 deletions buildpy/pylintrc

This file was deleted.

132 changes: 132 additions & 0 deletions buildpy/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# https://mypy.readthedocs.io/en/latest/config_file.html

[tool.mypy]

plugins = 'pydantic.mypy' # type: ignore[misc]

# Import discovery
follow_imports = 'silent'
follow_imports_for_stubs = true
no_silence_site_packages = true

# Disallow dynamic typing
disallow_any_decorated = true
disallow_any_explicit = true
disallow_any_generics = true

# Untyped definitions and calls
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
# NOTE(krishan711): this was causing errors with 0.990, uncomment once fixed
check_untyped_defs = true
disallow_untyped_decorators = true

# None and Optional handling
no_implicit_optional = true
strict_optional = true

# Configuring warnings
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_return_any = true
warn_unreachable = true

# Suppressing errors
ignore_errors = false

# Miscellaneous strictness flags
allow_untyped_globals = false
allow_redefinition = false
implicit_reexport = true
strict_equality = true


[tool.pydantic-mypy]

init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true


[tool.bandit]

exclude_dirs = ['tests']


[tool.pylint.main]
# http://pylint.pycqa.org/en/latest/technical_reference/features.html
extension-pkg-whitelist = ["pydantic", "math", "binascii"]
disable = [
"suppressed-message",
"missing-docstring",
"locally-disabled",
"file-ignored",
"fixme",
"line-too-long",
"len-as-condition",
"consider-iterating-dictionary",
"logging-fstring-interpolation",
"duplicate-code",
"f-string-without-interpolation",
"raise-missing-from",
"unspecified-encoding",
"superfluous-parens",
]
enable = ["useless-suppression"]
reports = false
score = false

# Naming conventions
[tool.pylint.basic]
# Allow underscores for variable names
good-names = "_"
variable-naming-style = "camelCase"
include-naming-hint = true
# Regex patterns for naming conventions
class-attribute-rgx = "^([a-z_][A-Za-z0-9]{1,30}|[A-Z_][A-Z0-9_]{1,30}|(__.*__))$"
variable-rgx = "^[a-z][a-zA-Z0-9]{1,50}$"
module-rgx = "^[a-z][a-z0-9_]*$"
class-rgx = "^[A-Z][a-zA-Z0-9]+$"
const-rgx = "^(([A-Z_][A-Z0-9_]*)|(__.*__))$"
attr-rgx = "^[a-z_][a-zA-Z0-9]{1,50}$"
argument-rgx = "^[a-z][a-zA-Z0-9]{1,30}$"
inlinevar-rgx = "^[a-z_][A-Za-z0-9]*$"
method-rgx = "^[a-z_][a-z0-9_]{2,60}$"
function-rgx = "^[a-z_][a-z0-9_]{2,60}$"
no-docstring-rgx = ".*" # Disable this if you want a minimum docstring length
docstring-min-length = 0 # Adjust if you want a minimum docstring length

# Formatting
[tool.pylint.format]
# Allow long lines (adjust if desired)
ignore-long-lines = ".*"
# Enforce single-line if/class statements (adjust as needed)
single-line-if-stmt = false
single-line-class-stmt = false

# Miscellaneous
[tool.pylint.miscellaneous]
# Allow comments with 'FIXME', 'NOTE', and 'TODO'
notes = ["FIXME", "NOTE", "TODO"]

# Similarities
[tool.pylint.similarities]
# Adjust minimum similarity lines for code blocks (optional)
min-similarity-lines = 4
# Ignore imports during similarity checks (adjust if needed)
ignore-imports = true

# Code complexity (adjust limits for your project's needs)
[tool.pylint.design]
max-args = 100
max-locals = 100
max-returns = 100
max-branches = 100
max-statements = 100
max-parents = 100
max-attributes = 100
min-public-methods = 0
max-public-methods = 100
max-bool-expr = 100
3 changes: 2 additions & 1 deletion buildpy/security_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def parse_messages(self, rawMessages: List[str]) -> List[Message]:
rawMessage = rawMessage.strip()
if len(rawMessage) == 0 or rawMessage.startswith('filename,test_name,'):
continue
print(rawMessage)
rawMessageParts = rawMessage.split('\t')
if len(rawMessageParts) == 1:
continue
Expand Down Expand Up @@ -134,7 +135,7 @@ def parse_messages(self, rawMessages: List[str]) -> List[Message]:
def run(targets: List[str], outputFilename: str, outputFormat: str, configFilePath: str) -> None:
currentDirectory = os.path.dirname(os.path.realpath(__file__))
messages: List[str] = []
banditConfigFilePath = configFilePath or f'{currentDirectory}/bandit.yaml'
banditConfigFilePath = configFilePath or f'{currentDirectory}/pyproject.toml'
# TODO(krishan711): use test_name here once enabled https://github.com/PyCQA/bandit/issues/962
messageTemplate = '{abspath}\t{line}\t{col}\t{test_id}\t{msg}\t{severity}'
try:
Expand Down
5 changes: 4 additions & 1 deletion buildpy/type_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def parse_messages(self, rawMessages: List[str]) -> List[Message]:
or ' note: ' in rawMessage \
or 'Use "-> None" if function does not return a value' in rawMessage:
continue
if 'Error importing plugin "pydantic.mypy":' in rawMessage:
# NOTE(krishan711): we use pydantic rules but it may not be installed (like in this repo)
continue
match1 = re.match(r'(.*):(\d*):(\d*): (.*): (.*) \[(.*)\]', rawMessage)
match2 = None
if not match1:
Expand All @@ -44,7 +47,7 @@ def parse_messages(self, rawMessages: List[str]) -> List[Message]:
@click.option('-c', '--config-file-path', 'configFilePath', required=False, type=str)
def run(targets: List[str], outputFilename: str, outputFormat: str, configFilePath: str) -> None:
currentDirectory = os.path.dirname(os.path.realpath(__file__))
mypyConfigFilePath = configFilePath or f'{currentDirectory}/mypy.ini'
mypyConfigFilePath = configFilePath or f'{currentDirectory}/pyproject.toml'
messages: List[str] = []
try:
subprocess.check_output(f'mypy {" ".join(targets)} --config-file {mypyConfigFilePath} --no-color-output --hide-error-context --no-pretty --no-error-summary --show-error-codes --show-column-numbers', stderr=subprocess.STDOUT, shell=True) # nosec=subprocess_popen_with_shell_equals_true
Expand Down
7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
package_data={
'buildpy': [
'py.typed',
'pylintrc',
'mypy.ini',
'bandit.yaml',
'pyproject.toml',
]
},
include_package_data=True,
Expand All @@ -34,7 +32,8 @@
},
entry_points='''
[console_scripts]
lint=buildpy.lint:run
lint=buildpy.lint_check:run
lint-check=buildpy.lint_check:run
type-check=buildpy.type_check:run
security-check=buildpy.security_check:run
version=buildpy.version:run
Expand Down
Loading