Skip to content

Conversation

@Akuli
Copy link
Collaborator

@Akuli Akuli commented May 15, 2021

This is basically the Python 3 version of #5451

I assumed that any Python 3 version being used is at least 3.6. Therefore, a check like sys.version_info >= (3, 6), or the same with any lower version, is always true.

Quick and dirty script
import re
import pathlib


def dedent_callback(match):
    replacement = match.group(2)
    if replacement:
        assert replacement.endswith('\n')
    return re.sub('^    ', '', replacement, flags=re.MULTILINE)


# Version checks can be nested
def sub_until_done(regex, replacement, text):
    while True:
        old_text = text
        text = re.sub(regex, replacement, text, flags=re.MULTILINE)
        if text == old_text:
            return text


for path in pathlib.Path('stdlib').rglob('*.pyi'):
    if '@python2' in path.parts:
        continue

    text = path.read_text()

    surely_this_or_newer_list = [
        '(3,)',
        '(3, 0)',
        '(3, 1)',
        '(3, 2)',
        '(3, 3)',
        '(3, 4)',
        '(3, 5)',
        '(3, 6)',
    ]
    for version in surely_this_or_newer_list:
        text = text.replace(f'sys.version_info >= {version}', 'True')
        text = text.replace(f'sys.version_info < {version}', 'False')

    text = text.replace('True and ', '')
    text = text.replace(' and True', '')
    text = text.replace('False or ', '')
    text = text.replace(' or False', '')
    text = re.sub(r'True or sys.platform .. "[^"\n]+"', 'True', text)

    text = sub_until_done(
        (
            r'^( *)if False:\n(?:\1    .*\n|\n)+'
            r'\1else:\n((?:\1    .*\n|\n)+)'
        ),
        dedent_callback, text
    )
    text = sub_until_done(
        (
            r'^( *)if False:\n(?:\1    .*\n|\n)+'
            r'\1el(if )'
        ),
        r'\1\2', text
    )
    text = sub_until_done(r'^( *)if False:\n(?:\1    .*\n|\n)+', r'', text)
    text = sub_until_done(r'^( *)elif False:\n(\1    .*\n|\n)+', '', text)
    text = sub_until_done(
        (
            r'^( *)if True:\n((?:\1    .*\n|\n)+)'
            r'(\1elif .+:\n(\1    .*\n|\n)+)*'
            r'(\1else:\n(\1    .*\n|\n)+)?'
        ),
        dedent_callback, text
    )
    text = sub_until_done(
        (
            r'^( *)elif True:\n((?:\1    .*\n|\n)+)'
            r'(\1elif .+:\n(\1    .*\n|\n)+)*'
            r'(\1else:\n(\1    .*\n|\n)+)?'
        ),
        r'\1else:\n\2', text
    )

    assert 'if True' not in text, path
    assert 'if False' not in text, path
    path.write_text(text)

else:
_Text = Union[str, unicode]

_Text = str
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Many modules seem to have aliases like this. Perhaps we should clean them up in another PR.

@Akuli
Copy link
Collaborator Author

Akuli commented May 15, 2021

Was part of #5461

@Akuli Akuli closed this May 15, 2021
@Akuli Akuli deleted the attempt3 branch May 15, 2021 12:34
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.

1 participant