Skip to content

Use lowercase type everywhere - #6853

Merged
srittau merged 6 commits into
python:masterfrom
AlexWaygood:lowercase-type
Jan 8, 2022
Merged

Use lowercase type everywhere#6853
srittau merged 6 commits into
python:masterfrom
AlexWaygood:lowercase-type

Conversation

@AlexWaygood

Copy link
Copy Markdown
Member
This PR was prepared using the following script:
importastimportreimportsubprocessimportsysfromitertoolsimportchainfrompathlibimportPathfromtypingimportNamedTupleclassDeleteableImport(NamedTuple):
old: strreplacement: strFAILURES= []
deffix_bad_syntax(path: Path) ->None:
withopen(path) asf:
stub=f.read()
lines=stub.splitlines()
tree=ast.parse(stub)
imports_to_delete= {}
type_from_typing=FalseclassBadImportFinder(ast.NodeVisitor):
defvisit_ImportFrom(self, node: ast.ImportFrom) ->None:
nonlocaltype_from_typingifnode.module!="typing":
returnifany(cls.name=="Type"forclsinnode.names):
type_from_typing=Trueelse:
returnnew_import_list= [clsforclsinnode.namesifcls.name!="Type"]
### DEALING WITH EXISTING IMPORT STATEMENTS #### Scenario (1): Now we don't need *any* imports from typing any more.# Don't touch the imports in collections/__init__.pyi as it causes a mypy crash (don't know why)ifnotnew_import_listandpath!=Path("stdlib/collections/__init__.pyi"):
imports_to_delete[node.lineno-1] =DeleteableImport(old=ast.unparse(node), replacement="")
# Scenario (2): we still need imports from typing; the existing import statement is only one lineelifnode.lineno==node.end_lineno:
imports_to_delete[node.lineno-1] =DeleteableImport(
old=ast.unparse(node),
replacement=ast.unparse(ast.ImportFrom(module="typing", names=new_import_list, level=0)),
)
# Scenario (3): we still need imports from typing; the existing import statement is multiline.else:
forclsinnode.names:
ifcls.name=="Type":
imports_to_delete[cls.lineno-1] =DeleteableImport(
old=f"Type,"ifcls.asnameisNoneelsef"Type as {cls.asname},", replacement=""
)
BadImportFinder().visit(tree)
ifnottype_from_typing:
returnforlineno, (old_syntax, new_syntax) inimports_to_delete.items():
lines[lineno] =lines[lineno].replace(old_syntax, new_syntax)
try:
new_tree=ast.parse("\n".join(lines))
exceptSyntaxError:
sys.stderr.write(f"Error converting new syntax in {path}")
FAILURES.append(path)
else:
lines_with_bad_syntax= []
classOldSyntaxFinder(ast.NodeVisitor):
defvisit_Subscript(self, node: ast.Subscript) ->None:
ifisinstance(node.value, ast.Name) andnode.value.id=="Type":
lines_with_bad_syntax.append(node.lineno-1)
self.generic_visit(node)
OldSyntaxFinder().visit(new_tree)
forlinenoinlines_with_bad_syntax:
lines[lineno] =re.sub(fr"(\W)Type\[", fr"\1type[", lines[lineno])
withopen(path, "w") asf:
f.write("\n".join(lines) +"\n")
defmain() ->None:
print("STARTING RUN: Will attempt to fix new syntax in typeshed directory...\n\n")
forpathinchain(Path("stdlib").rglob("*.pyi"), Path("stubs").rglob("*.pyi")):
if"@python2"inpath.parts:
print(f"Skipping {path}: Python-2 stub")
elifPath("stubs/protobuf/google/protobuf") inpath.parents:
print(f"Skipping {path}: protobuf stub")
else:
print(f"Attempting to convert {path} to new syntax.")
fix_bad_syntax(path)
print("\n\nSTARTING ISORT...\n\n")
forfolderin {"stdlib", "stubs", "tests"}:
subprocess.run([sys.executable, "-m", "isort", folder])
print("\n\nSTARTING BLACK...\n\n")
subprocess.run([sys.executable, "-m", "black", "."])
ifFAILURES:
print("\n\nFAILED to convert the following files to new syntax:\n")
forpathinFAILURES:
print(f"- {path}")
else:
print("\n\nThere were ZERO failures in converting to new syntax. HOORAY!!\n\n")
print('\n\nRunning "check_new_syntax.py"...\n\n')
subprocess.run([sys.executable, "tests/check_new_syntax.py"])
print('\n\nRunning "stubtest_stdlib.py"...\n\n')
subprocess.run([sys.executable, "tests/stubtest_stdlib.py"])
if__name__=="__main__":
main()

@github-actions

This comment has been minimized.

2 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@AlexWaygood

Copy link
Copy Markdown
MemberAuthor

I think the third-party stubtest failures for requests are unrelated to this PR?

@AlexWaygood

Copy link
Copy Markdown
MemberAuthor

The only change to requests stubs that this PR makes is in stubs/requests/requests/models.pyi, and that looks fine to me.

@hauntsaninja

Copy link
Copy Markdown
Collaborator

Looks like it's failing on master too, https://github.com/python/typeshed/actions/runs/1669875816
requests 2.27.1 was released in the last two days, so maybe that's it

@srittau

Copy link
Copy Markdown
Collaborator

#6858 for part 1 for a requests fix.

@srittausrittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I did a spot check and this looks good.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau
srittau merged commit a40d79a into python:masterJan 8, 2022
@AlexWaygood
AlexWaygood deleted the lowercase-type branch January 8, 2022 15:32
@AlexWaygood

Copy link
Copy Markdown
MemberAuthor

Thanks for the mypy fix @sobolevn 😀

@sobolevn

Copy link
Copy Markdown
Member

Happy to help! 😉

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@AlexWaygood@hauntsaninja@srittau@sobolevn