Skip to content

Big diff: use lower-case list and dict #5888

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

Merged
merged 26 commits into from
Aug 8, 2021
Merged

Conversation

Akuli
Copy link
Collaborator

@Akuli Akuli commented Aug 8, 2021

Created with quick and dirty scripts very similarly to #5872

main script

import sys
import pathlib
import re


def parse_list(string):
    assert string.startswith("[")
    depth = 1
    parts = []
    part_start = i = 1
    while depth > 0:
        if depth == 1 and string[i:].startswith(","):
            parts.append(string[part_start:i].strip())
            part_start = i + 1
        depth += string[i] == "["
        depth -= string[i] == "]"
        i += 1
    if string[part_start : i - 1].strip():
        parts.append(string[part_start : i - 1].strip())
    assert parts, string[:100]
    assert "" not in parts
    return (parts, i)


assert parse_list("[x] bruh") == (["x"], 3)
assert parse_list("[x, y[z]] bruh") == (["x", "y[z]"], 9)

for path in list(map(pathlib.Path, sys.argv[1:])) or (
    list(pathlib.Path("stdlib").rglob("*.pyi")) + list(pathlib.Path("stubs").rglob("*.pyi"))
):
    if "@python2" in path.parts:
        # no real reason to skip, I just don't like python 2
        continue

    print(path)
    content = path.read_text()

    while True:
        try:
            match = next(
                m for m in re.finditer(
                    r"^.*?\b(List|Dict|typing\.List|typing\.Dict)\[", content, flags=re.MULTILINE
                )
                # skip type aliases and base classes
                if not re.match("[A-Za-z_][A-Za-z0-9_]* = |class ", m.group(0).lstrip(" "))
            )
        except StopIteration:
            break

        parts, list_length = parse_list(content[match.end() - 1 :])
        start = match.start(1)
        end = match.end() - 1 + list_length
        if match.group(1).endswith("List"):
            [p] = parts
            content = content[:start] + f"list[{p}]" + content[end:]
        else:
            [k, v] = parts
            content = content[:start] + f"dict[{k}, {v}]" + content[end:]

    path.write_text(content)

import cleaner (can do weird things, needs manual review)

import pathlib
import re


for path in list(pathlib.Path("stdlib").rglob("*.pyi")) + list(pathlib.Path("stubs").rglob("*.pyi")):
    if "@python2" in path.parts:
        # no real reason to skip, I just don't like python 2
        continue

    print(path)
    content = path.read_text()

    for old in ["List", "Dict"]:
        if old + "[" not in content:
            content = re.sub(rf", \b{old}\b", "", content)
            content = re.sub(rf"\b{old}\b,", "", content)
            content = content.replace(r"from typing import " + old + "\n", "")
    path.write_text(content)

reverting script

#!/bin/bash
set -ex
[ -f $1 ]
git diff --exit-code
git diff HEAD --exit-code
git show origin/master:$1 > $1
git add $1
git commit -m "Revert $1 to origin/master"

Files that need manual work (checked = separate PR created):

  • stdlib/imaplib.pyi
  • stdlib/multiprocessing/managers.pyi
  • stdlib/nntplib.pyi
  • stdlib/os/__init__.pyi
  • stdlib/plistlib.pyi
  • stdlib/poplib.pyi
  • stdlib/pydoc.pyi
  • stdlib/tarfile.pyi
  • stdlib/xmlrpc/server.pyi
  • stubs/decorator/decorator.pyi
  • stubs/docutils/docutils/parsers/rst/roles.pyi (nothing to do)
  • stubs/frozendict/frozendict.pyi
  • stubs/Jinja2/jinja2/nodes.pyi
  • stubs/Pygments/pygments/token.pyi
  • stubs/requests/requests/models.pyi
  • stubs/Werkzeug/werkzeug/http.pyi

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

2 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

2 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Akuli Akuli marked this pull request as ready for review August 8, 2021 15:51
@github-actions
Copy link
Contributor

github-actions bot commented Aug 8, 2021

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

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to 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.

3 participants