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

Fix the --safe argument #52

Merged
merged 1 commit into from
Jan 20, 2023
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
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,28 @@ There are two shortcut flags to enable multiple transformations at once:

# Changelog

22.9.0 (September 5, 2022)
## Unreleased

- Add `--guess-common-names` (contributed by John Litborn)
- Fix the `--safe` and `--aggressive` flags so they don't take
ignored arguments
- `--length-hint` should return `int` (contributed by Nikita Sobolev)
- Fix bug in import adding (contributed by Shantanu)

## 22.9.0 (September 5, 2022)

- Add `--safe` and `--aggressive`
- Add `--pyanalyze-report`
- Do not add `None` return types to methods marked with `@abstractmethod` and
to methods in stub files
- Improve type inference:

- `"string" % ...` is always `str`
- `b"bytes" % ...` is always `bytes`
- An `and` or `or` operator where left and right sides are of the same type
returns that type
- `is`, `is not`, `in`, and `not in` always return `bool`

21.12.0 (December 21, 2021)
## 21.12.0 (December 21, 2021)

- Initial PyPI release
68 changes: 26 additions & 42 deletions autotyping/autotyping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
from typing import Dict, List, Optional, Sequence, Set, Tuple, Type
from typing_extensions import TypedDict
import re

import libcst
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand
Expand Down Expand Up @@ -90,37 +89,6 @@ class State:
}


class _SafeAction(argparse.Action):
def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: object,
option_string: Optional[str] = ...,
) -> None:
namespace.none_return = True
namespace.scalar_return = True
namespace.annotate_magics = True


class _AggressiveAction(_SafeAction):
def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: object,
option_string: Optional[str] = ...,
) -> None:
super().__call__(parser, namespace, values, option_string)
namespace.bool_param = True
namespace.int_param = True
namespace.float_param = True
namespace.str_param = True
namespace.bytes_param = True
namespace.annotate_imprecise_magics = True
namespace.guess_common_names = True


class AutotypeCommand(VisitorBasedCodemodCommand):
# Add a description so that future codemodders can see what this does.
DESCRIPTION: str = "Automatically adds simple type annotations."
Expand Down Expand Up @@ -230,15 +198,15 @@ def add_args(arg_parser: argparse.ArgumentParser) -> None:
)
arg_parser.add_argument(
"--safe",
action=_SafeAction,
action="store_true",
default=False,
help="Apply all safe transformations",
nargs="?",
)
arg_parser.add_argument(
"--aggressive",
action=_AggressiveAction,
action="store_true",
default=False,
help="Apply all transformations that do not require arguments",
nargs="?",
)

def __init__(
Expand All @@ -262,6 +230,18 @@ def __init__(
safe: bool = False,
aggressive: bool = False,
) -> None:
if safe or aggressive:
none_return = True
scalar_return = True
annotate_magics = True
if aggressive:
bool_param = True
int_param = True
float_param = True
str_param = True
bytes_param = True
annotate_imprecise_magics = True
guess_common_names = True
super().__init__(context)
param_type_pairs = [
(bool_param, bool),
Expand Down Expand Up @@ -295,12 +275,16 @@ def __init__(
)
] = metadata
self.state = State(
annotate_optionals=[NamedParam.make(s) for s in annotate_optional]
if annotate_optional
else [],
annotate_named_params=[NamedParam.make(s) for s in annotate_named_param]
if annotate_named_param
else [],
annotate_optionals=(
[NamedParam.make(s) for s in annotate_optional]
if annotate_optional
else []
),
annotate_named_params=(
[NamedParam.make(s) for s in annotate_named_param]
if annotate_named_param
else []
),
none_return=none_return,
scalar_return=scalar_return,
param_types={typ for param, typ in param_type_pairs if param},
Expand Down
Loading