Skip to content

Commit f84e2cf

Browse files
authored
Remove all builtins.unicode references (#13272)
1 parent 222ac7b commit f84e2cf

File tree

10 files changed

+15
-164
lines changed

10 files changed

+15
-164
lines changed

docs/source/mypy_daemon.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,6 @@ command.
228228
Only allow some fraction of types in the suggested signature to be ``Any`` types.
229229
The fraction ranges from ``0`` (same as ``--no-any``) to ``1``.
230230

231-
.. option:: --try-text
232-
233-
Try also using ``unicode`` wherever ``str`` is inferred. This flag may be useful
234-
for annotating Python 2/3 straddling code.
235-
236231
.. option:: --callsites
237232

238233
Only find call sites for a given function instead of suggesting a type.

mypy/dmypy/client.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ def __init__(self, prog: str) -> None:
161161
type=float,
162162
help="Allow anys in types if they go above a certain score (scores are from 0-1)",
163163
)
164-
p.add_argument(
165-
"--try-text", action="store_true", help="Try using unicode wherever str is inferred"
166-
)
167164
p.add_argument(
168165
"--callsites", action="store_true", help="Find callsites instead of suggesting a type"
169166
)
@@ -525,7 +522,6 @@ def do_suggest(args: argparse.Namespace) -> None:
525522
no_errors=args.no_errors,
526523
no_any=args.no_any,
527524
flex_any=args.flex_any,
528-
try_text=args.try_text,
529525
use_fixme=args.use_fixme,
530526
max_guesses=args.max_guesses,
531527
)

mypy/exprtotype.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,9 @@ def expr_to_unanalyzed_type(
168168
column=expr.column,
169169
)
170170
elif isinstance(expr, StrExpr):
171-
return parse_type_string(
172-
expr.value, "builtins.str", expr.line, expr.column, assume_str_is_unicode=True
173-
)
171+
return parse_type_string(expr.value, "builtins.str", expr.line, expr.column)
174172
elif isinstance(expr, BytesExpr):
175-
return parse_type_string(
176-
expr.value, "builtins.bytes", expr.line, expr.column, assume_str_is_unicode=False
177-
)
173+
return parse_type_string(expr.value, "builtins.bytes", expr.line, expr.column)
178174
elif isinstance(expr, UnaryExpr):
179175
typ = expr_to_unanalyzed_type(expr.expr, options, allow_new_syntax)
180176
if isinstance(typ, RawExpressionType):

mypy/fastparse.py

Lines changed: 7 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,7 @@ def parse_type_ignore_tag(tag: Optional[str]) -> Optional[List[str]]:
331331

332332

333333
def parse_type_comment(
334-
type_comment: str,
335-
line: int,
336-
column: int,
337-
errors: Optional[Errors],
338-
assume_str_is_unicode: bool = True,
334+
type_comment: str, line: int, column: int, errors: Optional[Errors]
339335
) -> Tuple[Optional[List[str]], Optional[ProperType]]:
340336
"""Parse type portion of a type comment (+ optional type ignore).
341337
@@ -366,44 +362,21 @@ def parse_type_comment(
366362
ignored = None
367363
assert isinstance(typ, ast3_Expression)
368364
converted = TypeConverter(
369-
errors,
370-
line=line,
371-
override_column=column,
372-
assume_str_is_unicode=assume_str_is_unicode,
373-
is_evaluated=False,
365+
errors, line=line, override_column=column, is_evaluated=False
374366
).visit(typ.body)
375367
return ignored, converted
376368

377369

378370
def parse_type_string(
379-
expr_string: str,
380-
expr_fallback_name: str,
381-
line: int,
382-
column: int,
383-
assume_str_is_unicode: bool = True,
371+
expr_string: str, expr_fallback_name: str, line: int, column: int
384372
) -> ProperType:
385-
"""Parses a type that was originally present inside of an explicit string,
386-
byte string, or unicode string.
373+
"""Parses a type that was originally present inside of an explicit string.
387374
388375
For example, suppose we have the type `Foo["blah"]`. We should parse the
389376
string expression "blah" using this function.
390-
391-
If `assume_str_is_unicode` is set to true, this function will assume that
392-
`Foo["blah"]` is equivalent to `Foo[u"blah"]`. Otherwise, it assumes it's
393-
equivalent to `Foo[b"blah"]`.
394-
395-
The caller is responsible for keeping track of the context in which the
396-
type string was encountered (e.g. in Python 3 code, Python 2 code, Python 2
397-
code with unicode_literals...) and setting `assume_str_is_unicode` accordingly.
398377
"""
399378
try:
400-
_, node = parse_type_comment(
401-
expr_string.strip(),
402-
line=line,
403-
column=column,
404-
errors=None,
405-
assume_str_is_unicode=assume_str_is_unicode,
406-
)
379+
_, node = parse_type_comment(expr_string.strip(), line=line, column=column, errors=None)
407380
if isinstance(node, UnboundType) and node.original_str_expr is None:
408381
node.original_str_expr = expr_string
409382
node.original_str_fallback = expr_fallback_name
@@ -1743,14 +1716,12 @@ def __init__(
17431716
errors: Optional[Errors],
17441717
line: int = -1,
17451718
override_column: int = -1,
1746-
assume_str_is_unicode: bool = True,
17471719
is_evaluated: bool = True,
17481720
) -> None:
17491721
self.errors = errors
17501722
self.line = line
17511723
self.override_column = override_column
17521724
self.node_stack: List[AST] = []
1753-
self.assume_str_is_unicode = assume_str_is_unicode
17541725
self.is_evaluated = is_evaluated
17551726

17561727
def convert_column(self, column: int) -> int:
@@ -1921,22 +1892,7 @@ def visit_Constant(self, n: Constant) -> Type:
19211892
return UnboundType("None", line=self.line)
19221893
if isinstance(val, str):
19231894
# Parse forward reference.
1924-
if (n.kind and "u" in n.kind) or self.assume_str_is_unicode:
1925-
return parse_type_string(
1926-
n.s,
1927-
"builtins.unicode",
1928-
self.line,
1929-
n.col_offset,
1930-
assume_str_is_unicode=self.assume_str_is_unicode,
1931-
)
1932-
else:
1933-
return parse_type_string(
1934-
n.s,
1935-
"builtins.str",
1936-
self.line,
1937-
n.col_offset,
1938-
assume_str_is_unicode=self.assume_str_is_unicode,
1939-
)
1895+
return parse_type_string(n.s, "builtins.str", self.line, n.col_offset)
19401896
if val is Ellipsis:
19411897
# '...' is valid in some types.
19421898
return EllipsisType(line=self.line)
@@ -1990,34 +1946,7 @@ def visit_Num(self, n: Num) -> Type:
19901946

19911947
# Str(string s)
19921948
def visit_Str(self, n: Str) -> Type:
1993-
# Note: we transform these fallback types into the correct types in
1994-
# 'typeanal.py' -- specifically in the named_type_with_normalized_str method.
1995-
# If we're analyzing Python 3, that function will translate 'builtins.unicode'
1996-
# into 'builtins.str'. In contrast, if we're analyzing Python 2 code, we'll
1997-
# translate 'builtins.bytes' in the method below into 'builtins.str'.
1998-
1999-
# Do a getattr because the field doesn't exist in 3.8 (where
2000-
# this method doesn't actually ever run.) We can't just do
2001-
# an attribute access with a `# type: ignore` because it would be
2002-
# unused on < 3.8.
2003-
kind: str = getattr(n, "kind") # noqa
2004-
2005-
if "u" in kind or self.assume_str_is_unicode:
2006-
return parse_type_string(
2007-
n.s,
2008-
"builtins.unicode",
2009-
self.line,
2010-
n.col_offset,
2011-
assume_str_is_unicode=self.assume_str_is_unicode,
2012-
)
2013-
else:
2014-
return parse_type_string(
2015-
n.s,
2016-
"builtins.str",
2017-
self.line,
2018-
n.col_offset,
2019-
assume_str_is_unicode=self.assume_str_is_unicode,
2020-
)
1949+
return parse_type_string(n.s, "builtins.str", self.line, n.col_offset)
20211950

20221951
# Bytes(bytes s)
20231952
def visit_Bytes(self, n: Bytes) -> Type:

mypy/plugins/ctypes.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,6 @@
2323
)
2424

2525

26-
def _get_bytes_type(api: "mypy.plugin.CheckerPluginInterface") -> Instance:
27-
"""Return the type corresponding to bytes on the current Python version.
28-
29-
This is bytes in Python 3, and str in Python 2.
30-
"""
31-
return api.named_generic_type(
32-
"builtins.bytes" if api.options.python_version >= (3,) else "builtins.str", []
33-
)
34-
35-
36-
def _get_text_type(api: "mypy.plugin.CheckerPluginInterface") -> Instance:
37-
"""Return the type corresponding to Text on the current Python version.
38-
39-
This is str in Python 3, and unicode in Python 2.
40-
"""
41-
return api.named_generic_type(
42-
"builtins.str" if api.options.python_version >= (3,) else "builtins.unicode", []
43-
)
44-
45-
4626
def _find_simplecdata_base_arg(
4727
tp: Instance, api: "mypy.plugin.CheckerPluginInterface"
4828
) -> Optional[ProperType]:
@@ -221,9 +201,9 @@ def array_value_callback(ctx: "mypy.plugin.AttributeContext") -> Type:
221201
if isinstance(tp, AnyType):
222202
types.append(AnyType(TypeOfAny.from_another_any, source_any=tp))
223203
elif isinstance(tp, Instance) and tp.type.fullname == "ctypes.c_char":
224-
types.append(_get_bytes_type(ctx.api))
204+
types.append(ctx.api.named_generic_type("builtins.bytes", []))
225205
elif isinstance(tp, Instance) and tp.type.fullname == "ctypes.c_wchar":
226-
types.append(_get_text_type(ctx.api))
206+
types.append(ctx.api.named_generic_type("builtins.str", []))
227207
else:
228208
ctx.api.msg.fail(
229209
'Array attribute "value" is only available'
@@ -245,7 +225,7 @@ def array_raw_callback(ctx: "mypy.plugin.AttributeContext") -> Type:
245225
or isinstance(tp, Instance)
246226
and tp.type.fullname == "ctypes.c_char"
247227
):
248-
types.append(_get_bytes_type(ctx.api))
228+
types.append(ctx.api.named_generic_type("builtins.bytes", []))
249229
else:
250230
ctx.api.msg.fail(
251231
'Array attribute "raw" is only available'

mypy/stubgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def args_str(self, args: Iterable[Type]) -> str:
308308
The main difference from list_str is the preservation of quotes for string
309309
arguments
310310
"""
311-
types = ["builtins.bytes", "builtins.unicode"]
311+
types = ["builtins.bytes", "builtins.str"]
312312
res = []
313313
for arg in args:
314314
arg_str = arg.accept(self)

mypy/suggestions.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ def __init__(
249249
json: bool,
250250
no_errors: bool = False,
251251
no_any: bool = False,
252-
try_text: bool = False,
253252
flex_any: Optional[float] = None,
254253
use_fixme: Optional[str] = None,
255254
max_guesses: Optional[int] = None,
@@ -262,7 +261,6 @@ def __init__(
262261

263262
self.give_json = json
264263
self.no_errors = no_errors
265-
self.try_text = try_text
266264
self.flex_any = flex_any
267265
if no_any:
268266
self.flex_any = 1.0
@@ -401,12 +399,6 @@ def get_default_arg_types(self, fdef: FuncDef) -> List[Optional[Type]]:
401399
for arg in fdef.arguments
402400
]
403401

404-
def add_adjustments(self, typs: List[Type]) -> List[Type]:
405-
if not self.try_text or self.manager.options.python_version[0] != 2:
406-
return typs
407-
translator = StrToText(self.named_type)
408-
return dedup(typs + [tp.accept(translator) for tp in typs])
409-
410402
def get_guesses(
411403
self,
412404
is_method: bool,
@@ -420,7 +412,6 @@ def get_guesses(
420412
This focuses just on the argument types, and doesn't change the provided return type.
421413
"""
422414
options = self.get_args(is_method, base, defaults, callsites, uses)
423-
options = [self.add_adjustments(tps) for tps in options]
424415

425416
# Take the first `max_guesses` guesses.
426417
product = itertools.islice(itertools.product(*options), 0, self.max_guesses)
@@ -775,8 +766,6 @@ def score_type(self, t: Type, arg_pos: bool) -> int:
775766
return 10
776767
if isinstance(t, CallableType) and (has_any_type(t) or is_tricky_callable(t)):
777768
return 10
778-
if self.try_text and isinstance(t, Instance) and t.type.fullname == "builtins.str":
779-
return 1
780769
return 0
781770

782771
def score_callable(self, t: CallableType) -> int:
@@ -909,23 +898,6 @@ def visit_callable_type(self, t: CallableType) -> str:
909898
return f"Callable[{arg_str}, {t.ret_type.accept(self)}]"
910899

911900

912-
class StrToText(TypeTranslator):
913-
def __init__(self, named_type: Callable[[str], Instance]) -> None:
914-
self.text_type = named_type("builtins.unicode")
915-
916-
def visit_type_alias_type(self, t: TypeAliasType) -> Type:
917-
exp_t = get_proper_type(t)
918-
if isinstance(exp_t, Instance) and exp_t.type.fullname == "builtins.str":
919-
return self.text_type
920-
return t.copy_modified(args=[a.accept(self) for a in t.args])
921-
922-
def visit_instance(self, t: Instance) -> Type:
923-
if t.type.fullname == "builtins.str":
924-
return self.text_type
925-
else:
926-
return super().visit_instance(t)
927-
928-
929901
TType = TypeVar("TType", bound=Type)
930902

931903

mypy/test/testfinegrained.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ def maybe_suggest(self, step: int, server: Server, src: str, tmp_dir: str) -> Li
292292
callsites = "--callsites" in flags
293293
no_any = "--no-any" in flags
294294
no_errors = "--no-errors" in flags
295-
try_text = "--try-text" in flags
296295
m = re.match("--flex-any=([0-9.]+)", flags)
297296
flex_any = float(m.group(1)) if m else None
298297
m = re.match(r"--use-fixme=(\w+)", flags)
@@ -304,7 +303,6 @@ def maybe_suggest(self, step: int, server: Server, src: str, tmp_dir: str) -> Li
304303
json=json,
305304
no_any=no_any,
306305
no_errors=no_errors,
307-
try_text=try_text,
308306
flex_any=flex_any,
309307
use_fixme=use_fixme,
310308
callsites=callsites,

mypy/typeanal.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ def analyze_literal_param(self, idx: int, arg: Type, ctx: Context) -> Optional[L
11621162
return [
11631163
LiteralType(
11641164
value=arg.original_str_expr,
1165-
fallback=self.named_type_with_normalized_str(arg.original_str_fallback),
1165+
fallback=self.named_type(arg.original_str_fallback),
11661166
line=arg.line,
11671167
column=arg.column,
11681168
)
@@ -1210,7 +1210,7 @@ def analyze_literal_param(self, idx: int, arg: Type, ctx: Context) -> Optional[L
12101210
return None
12111211

12121212
# Remap bytes and unicode into the appropriate type for the correct Python version
1213-
fallback = self.named_type_with_normalized_str(arg.base_type_name)
1213+
fallback = self.named_type(arg.base_type_name)
12141214
assert isinstance(fallback, Instance)
12151215
return [LiteralType(arg.literal_value, fallback, line=arg.line, column=arg.column)]
12161216
elif isinstance(arg, (NoneType, LiteralType)):
@@ -1357,17 +1357,6 @@ def anal_var_def(self, var_def: TypeVarLikeType) -> TypeVarLikeType:
13571357
def anal_var_defs(self, var_defs: Sequence[TypeVarLikeType]) -> List[TypeVarLikeType]:
13581358
return [self.anal_var_def(vd) for vd in var_defs]
13591359

1360-
def named_type_with_normalized_str(self, fully_qualified_name: str) -> Instance:
1361-
"""Does almost the same thing as `named_type`, except that we immediately
1362-
unalias `builtins.bytes` and `builtins.unicode` to `builtins.str` as appropriate.
1363-
"""
1364-
python_version = self.options.python_version
1365-
if python_version[0] == 2 and fully_qualified_name == "builtins.bytes":
1366-
fully_qualified_name = "builtins.str"
1367-
if python_version[0] >= 3 and fully_qualified_name == "builtins.unicode":
1368-
fully_qualified_name = "builtins.str"
1369-
return self.named_type(fully_qualified_name)
1370-
13711360
def named_type(
13721361
self,
13731362
fully_qualified_name: str,

mypy/types.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,10 +2406,6 @@ def value_repr(self) -> str:
24062406
# Note: 'builtins.bytes' only appears in Python 3, so we want to
24072407
# explicitly prefix with a "b"
24082408
return "b" + raw
2409-
elif fallback_name == "builtins.unicode":
2410-
# Similarly, 'builtins.unicode' only appears in Python 2, where we also
2411-
# want to explicitly prefix
2412-
return "u" + raw
24132409
else:
24142410
# 'builtins.str' could mean either depending on context, but either way
24152411
# we don't prefix: it's the "native" string. And of course, if value is

0 commit comments

Comments
 (0)