Skip to content

Commit

Permalink
Add disable_all option in pyproject.toml (#659)
Browse files Browse the repository at this point in the history
Co-authored-by: LGS-NET\FMAN <manuel.floriano-vazquez@hexagon.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
  • Loading branch information
3 people authored Sep 24, 2023
1 parent d526394 commit 201fbc9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Add option to disable all error codes (#659)
- Add hacky fix for bugs with hashability on type objects (#689)
- Show an error on calls to `typing.Any` (#688)
- Add command-line option `-c`/`--code` to typecheck code from
Expand Down
23 changes: 23 additions & 0 deletions pyanalyze/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import argparse
import functools
import pathlib
import sys
from collections import defaultdict
Expand All @@ -14,11 +15,13 @@
ClassVar,
Collection,
Dict,
FrozenSet,
Generic,
Iterable,
Mapping,
Optional,
Sequence,
Set,
Tuple,
Type,
TypeVar,
Expand Down Expand Up @@ -371,6 +374,11 @@ def parse_config_file(
)


@functools.lru_cache
def get_all_error_codes() -> FrozenSet[str]:
return frozenset({error_code.name for error_code in ErrorCode})


def _parse_config_section(
section: Mapping[str, Any],
module_path: ModulePath = (),
Expand All @@ -384,6 +392,11 @@ def _parse_config_section(
raise InvalidConfigOption(
"Top-level configuration should not set module option"
)

enabled_error_codes: Set[str] = set()
all_error_codes = get_all_error_codes()
disable_all_default_error_codes = False

for key, value in section.items():
if key == "module":
if module_path == ():
Expand Down Expand Up @@ -417,9 +430,19 @@ def _parse_config_section(
priority=priority,
seen_paths=seen_paths,
)
elif key == "disable_all":
disable_all_default_error_codes = value
else:
try:
option_cls = ConfigOption.registry[key]
except KeyError:
raise InvalidConfigOption(f"Invalid configuration option {key!r}")
if isinstance(value, bool) and key in all_error_codes and value is True:
enabled_error_codes.add(key)
yield option_cls(option_cls.parse(value, path), module_path)

if disable_all_default_error_codes:
error_codes_to_disable = all_error_codes - enabled_error_codes
for error_code in error_codes_to_disable:
option_cls = ConfigOption.registry[error_code]
yield option_cls(option_cls.parse(False, path), module_path)

0 comments on commit 201fbc9

Please sign in to comment.