Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b905a0

Browse files
Add --safe and --aggressive (#37)
Fixes #36
1 parent afb73cc commit 1b905a0

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,20 @@ more transformations. The following are supported:
7070
new imports. This is useful because suggestions that require imports may need
7171
more manual work.
7272

73-
Things to add:
73+
There are two shortcut flags to enable multiple transformations at once:
7474

75-
- Infer `-> bool` as the return type if all return statements are
76-
boolean expressions like `==`.
75+
- `--safe` enables changes that should always be safe. This includes
76+
`--none-return`, `--scalar-return`, and `--annotate-magics`.
77+
- `--aggressive` enables riskier changes that are more likely to produce
78+
new type checker errors. It includes all of `--safe` as well as `--bool-param`,
79+
`--int-param`, `--float-param`, `--str-param`, `--bytes-param`, and
80+
`--annotate-imprecise-magics`.
7781

7882
# Changelog
7983

8084
Unreleased
8185

86+
- Add `--safe` and `--aggressive`
8287
- Add `--pyanalyze-report`
8388
- Do not add `None` return types to methods marked with `@abstractmethod` and
8489
to methods in stub files

autotyping/autotyping.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ class State:
8585
}
8686

8787

88+
class _SafeAction(argparse.Action):
89+
def __call__(
90+
self,
91+
parser: argparse.ArgumentParser,
92+
namespace: argparse.Namespace,
93+
values: object,
94+
option_string: Optional[str] = ...,
95+
) -> None:
96+
namespace.none_return = True
97+
namespace.scalar_return = True
98+
namespace.annotate_magics = True
99+
100+
101+
class _AggressiveAction(_SafeAction):
102+
def __call__(
103+
self,
104+
parser: argparse.ArgumentParser,
105+
namespace: argparse.Namespace,
106+
values: object,
107+
option_string: Optional[str] = ...,
108+
) -> None:
109+
super().__call__(parser, namespace, values, option_string)
110+
namespace.bool_param = True
111+
namespace.int_param = True
112+
namespace.float_param = True
113+
namespace.str_param = True
114+
namespace.bytes_param = True
115+
namespace.annotate_imprecise_magics = True
116+
117+
88118
class AutotypeCommand(VisitorBasedCodemodCommand):
89119

90120
# Add a description so that future codemodders can see what this does.
@@ -187,6 +217,18 @@ def add_args(arg_parser: argparse.ArgumentParser) -> None:
187217
default=False,
188218
help="Only apply pyanalyze suggestions that do not require imports",
189219
)
220+
arg_parser.add_argument(
221+
"--safe",
222+
action=_SafeAction,
223+
help="Apply all safe transformations",
224+
nargs="?",
225+
)
226+
arg_parser.add_argument(
227+
"--aggressive",
228+
action=_AggressiveAction,
229+
help="Apply all transformations that do not require arguments",
230+
nargs="?",
231+
)
190232

191233
def __init__(
192234
self,
@@ -205,6 +247,8 @@ def __init__(
205247
int_param: bool = False,
206248
pyanalyze_report: Optional[str] = None,
207249
only_without_imports: bool = False,
250+
safe: bool = False,
251+
aggressive: bool = False,
208252
) -> None:
209253
super().__init__(context)
210254
param_type_pairs = [

0 commit comments

Comments
 (0)