Skip to content

Conversation

@Akuli
Copy link
Collaborator

@Akuli Akuli commented May 14, 2021

#5442 copied files

Done with quick and dirty script:

import pathlib
import re
import sys   # Needed for evil eval haxor


version_check_regex = r'''
^( *)if sys\.version_info (>=|>|<=|<|==) .+: *(#.*)?\n(\1    .*\n|\n)+
(\1elif sys\.version_info (>=|>|<=|<|==) .+: *(#.*)?\n(\1    .*\n|\n)+)*
(\1else: *(#.*)?\n((\1    .*\n|\n)+))?
'''.replace('\n', '')


def sub_callback(match):
    indent = match.group(1)
    cases = re.split('^' + indent + 'el', match.group(0), flags=re.MULTILINE)

    if cases[-1].startswith('se:'):
        ifs = cases[:-1]    # skip else
        the_else = '\n'.join(cases[-1].split('\n')[1:])
    else:
        ifs = cases
        the_else = ''

    for if_part in ifs:
        code = if_part.split('\n')[0].replace('if', '').replace(':', '')
        if eval(code.replace('sys.version_info', '(2, 7)').strip()):
            replacement = '\n'.join(if_part.split('\n')[1:])
            break
    else:
        replacement = the_else

    if replacement:
        assert replacement.endswith('\n')

    return re.sub('^    ', '', replacement, flags=re.MULTILINE)


for path in pathlib.Path('stdlib/@python2').rglob('*.pyi'):
    print(path)
    text = path.read_text()
    text = re.sub(version_check_regex, sub_callback, text, flags=re.MULTILINE)
    path.write_text(text)

@Akuli
Copy link
Collaborator Author

Akuli commented May 14, 2021

Script breaks elif, will fix

edit: done

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Only read as far as array

ReadableBuffer = Union[bytes, bytearray, memoryview, array.array[Any], mmap.mmap, buffer]
WriteableBuffer = Union[bytearray, memoryview, array.array[Any], mmap.mmap, buffer]

# Used by type checkers for checks involving None (does not exist at runtime)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Used by type checkers for checks involving None (does not exist at runtime)

It does exist in Python 2

$ python2
Python 2.7.12 (default, Nov 12 2018, 14:36:49) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import types
>>> types.NoneType
<type 'NoneType'>
>>> 

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, types.NoneType exists, but the comment is about _typeshed.NoneType.

Copy link
Member

Choose a reason for hiding this comment

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

My reading comprehension needs some work sometimes :)

In that case the comment still doesn't make much sense, since none of this file exists at runtime.

@@ -1,4 +1 @@
import sys
Copy link
Member

Choose a reason for hiding this comment

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

I guess you'll have to remove the import too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The Python 2 stubs don't get checked for missing imports. We have this in .flake8:

exclude = .venv*,@*,.git,*_pb2.pyi

Is this intentional?

Copy link
Member

Choose a reason for hiding this comment

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

Not sure what that's there for. It predates the @python2 directory, since it was there before 9af49c0. I think we can just remove it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Looking at git history, this is not intentional; ignoring @* was added before the big directory restructuring.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably remove it after this PR, since I expect many new errors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I started cleaning unused imports from Python 2 stubs, but I'd rather have that in a separate PR because it's a lot of manual work.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good. I think we should just merge this PR and follow up on other issues later.

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