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

ruff: add pyupgrade #16023

Merged
merged 8 commits into from
Sep 3, 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
4 changes: 2 additions & 2 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Callable, Optional, Sequence, cast
from typing import TYPE_CHECKING, Callable, Sequence, cast

from mypy import meet, message_registry, subtypes
from mypy.erasetype import erase_typevars
Expand Down Expand Up @@ -777,7 +777,7 @@ def analyze_var(
result: Type = t
typ = get_proper_type(typ)

call_type: Optional[ProperType] = None
call_type: ProperType | None = None
if var.is_initialized_in_class and (not is_instance_var(var) or mx.is_operator):
if isinstance(typ, FunctionLike) and not typ.is_type_obj():
call_type = typ
Expand Down
22 changes: 13 additions & 9 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,18 @@ def parse_config_file(
)
if report_dirs:
print(
"%sPer-module sections should not specify reports (%s)"
% (prefix, ", ".join(s + "_report" for s in sorted(report_dirs))),
prefix,
"Per-module sections should not specify reports ({})".format(
", ".join(s + "_report" for s in sorted(report_dirs))
),
file=stderr,
)
if set(updates) - PER_MODULE_OPTIONS:
print(
"%sPer-module sections should only specify per-module flags (%s)"
% (prefix, ", ".join(sorted(set(updates) - PER_MODULE_OPTIONS))),
prefix,
"Per-module sections should only specify per-module flags ({})".format(
", ".join(sorted(set(updates) - PER_MODULE_OPTIONS))
),
file=stderr,
)
updates = {k: v for k, v in updates.items() if k in PER_MODULE_OPTIONS}
Expand All @@ -315,8 +319,9 @@ def parse_config_file(
"*" in x and x != "*" for x in glob.split(".")
):
print(
"%sPatterns must be fully-qualified module names, optionally "
"with '*' in some components (e.g spam.*.eggs.*)" % prefix,
prefix,
"Patterns must be fully-qualified module names, optionally "
"with '*' in some components (e.g spam.*.eggs.*)",
file=stderr,
)
else:
Expand All @@ -329,7 +334,7 @@ def get_prefix(file_read: str, name: str) -> str:
else:
module_name_str = name

return f"{file_read}: [{module_name_str}]: "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of the change to this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before it was print("%sfoobar" % prefix), now it's print(prefix, "foobar") hence removed the space.

return f"{file_read}: [{module_name_str}]:"


def is_toml(filename: str) -> bool:
Expand Down Expand Up @@ -411,8 +416,7 @@ def destructure_overrides(toml_data: dict[str, Any]) -> dict[str, Any]:
raise ConfigTOMLValueError(
"toml config file contains "
"[[tool.mypy.overrides]] sections with conflicting "
"values. Module '%s' has two different values for '%s'"
% (module, new_key)
f"values. Module '{module}' has two different values for '{new_key}'"
)
result[old_config_name][new_key] = new_value

Expand Down
3 changes: 1 addition & 2 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ def flush_errors(new_messages: list[str], serious: bool) -> None:
and not options.non_interactive
):
print(
"Warning: unused section(s) in %s: %s"
% (
"Warning: unused section(s) in {}: {}".format(
options.config_file,
get_config_module_names(
options.config_file,
Expand Down
15 changes: 7 additions & 8 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,20 +1454,19 @@ def cannot_determine_type_in_base(self, name: str, base: str, context: Context)
self.fail(f'Cannot determine type of "{name}" in base class "{base}"', context)

def no_formal_self(self, name: str, item: CallableType, context: Context) -> None:
type = format_type(item, self.options)
self.fail(
'Attribute function "%s" with type %s does not accept self argument'
% (name, format_type(item, self.options)),
context,
f'Attribute function "{name}" with type {type} does not accept self argument', context
)

def incompatible_self_argument(
self, name: str, arg: Type, sig: CallableType, is_classmethod: bool, context: Context
) -> None:
kind = "class attribute function" if is_classmethod else "attribute function"
arg_type = format_type(arg, self.options)
sig_type = format_type(sig, self.options)
self.fail(
'Invalid self argument %s to %s "%s" with type %s'
% (format_type(arg, self.options), kind, name, format_type(sig, self.options)),
context,
f'Invalid self argument {arg_type} to {kind} "{name}" with type {sig_type}', context
)

def incompatible_conditional_function_def(
Expand All @@ -1487,8 +1486,8 @@ def cannot_instantiate_abstract_class(
) -> None:
attrs = format_string_list([f'"{a}"' for a in abstract_attributes])
self.fail(
'Cannot instantiate abstract class "%s" with abstract '
"attribute%s %s" % (class_name, plural_s(abstract_attributes), attrs),
f'Cannot instantiate abstract class "{class_name}" with abstract '
f"attribute{plural_s(abstract_attributes)} {attrs}",
context,
code=codes.ABSTRACT,
)
Expand Down
4 changes: 2 additions & 2 deletions mypy/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from collections import defaultdict
from typing import Iterable, Sequence, Tuple
from typing import Iterable, Sequence
from typing_extensions import TypeAlias as _TypeAlias

from mypy.constraints import SUBTYPE_OF, SUPERTYPE_OF, Constraint, infer_constraints
Expand Down Expand Up @@ -333,7 +333,7 @@ def is_trivial_bound(tp: ProperType) -> bool:
return isinstance(tp, Instance) and tp.type.fullname == "builtins.object"


def find_linear(c: Constraint) -> Tuple[bool, TypeVarId | None]:
def find_linear(c: Constraint) -> tuple[bool, TypeVarId | None]:
"""Find out if this constraint represent a linear relationship, return target id if yes."""
if isinstance(c.origin_type_var, TypeVarType):
if isinstance(c.target, TypeVarType):
Expand Down
2 changes: 1 addition & 1 deletion mypyc/ir/class_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@


class VTableMethod(NamedTuple):
cls: "ClassIR"
cls: "ClassIR" # noqa: UP037
name: str
method: FuncIR
shadow_method: FuncIR | None
Expand Down
4 changes: 2 additions & 2 deletions mypyc/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,5 +1575,5 @@ def visit_keep_alive(self, op: KeepAlive) -> T:
# (Serialization and deserialization *will* be used for incremental
# compilation but so far it is not hooked up to anything.)
class DeserMaps(NamedTuple):
classes: dict[str, "ClassIR"]
functions: dict[str, "FuncIR"]
classes: dict[str, ClassIR]
functions: dict[str, FuncIR]
4 changes: 2 additions & 2 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from __future__ import annotations

from abc import abstractmethod
from typing import TYPE_CHECKING, ClassVar, Generic, TypeVar
from typing_extensions import Final, TypeGuard
from typing import TYPE_CHECKING, ClassVar, Final, Generic, TypeVar
from typing_extensions import TypeGuard

from mypyc.common import IS_32_BIT_PLATFORM, PLATFORM_SIZE, JsonDict, short_name
from mypyc.namegen import NameGenerator
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ select = [
"B", # flake8-bugbear
"I", # isort
"RUF100", # Unused noqa comments
"PGH004" # blanket noqa comments
"PGH004", # blanket noqa comments
"UP", # pyupgrade
]

ignore = [
Expand All @@ -49,6 +50,7 @@ ignore = [
"E501", # conflicts with black
"E731", # Do not assign a `lambda` expression, use a `def`
"E741", # Ambiguous variable name
"UP032", # 'f-string always preferable to format' is controversial
]

unfixable = [
Expand Down