Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

replace type(x) == y checks by isinstance(x, y) and clean up resulting code #120

Merged
merged 7 commits into from Mar 26, 2020
Merged

replace type(x) == y checks by isinstance(x, y) and clean up resulting code #120

merged 7 commits into from Mar 26, 2020

Commits on Mar 15, 2020

  1. replace type(x) == type(y) checks by isinstance

    Especially, replace things like `type(x) == type([])` by `isinstance(list)`.
    
    Also do `x is None` special casing.
    
    Using the following py3 script:
    
    import pathlib
    import re
    import sys
    
    simple = {
        r"\[\]": r"list",
        r"\(\)": r"tuple",
        r"\(\d+,\)": r"tuple",
        r"\{\}": r"dict",
        r"\d+": r"int",
        r"''": r"str",
        r'""': r"str",
        r"long\(\d+\)": r"long",
    }
    
    others = {
        r"type\((.*?)\)\s*==\s*type\(\s*None\s*\)": r"\1 is None",
        r"type\((.*?)\)\s*!=\s*type\(\s*None\s*\)": r"\1 is not None",
        r"type\((.*?)\)\s*==\s*(type\(.*?\))": r"isinstance(\1, \2)",
        r"type\((.*?)\)\s*!=\s*(type\(.*?\))": r"not isinstance(\1, \2)",
    }
    
    def replace_in_all_files(root, pat, sub):
        for path in root.glob("**/*.py"):
            with path.open() as file:
                data = file.read()
            new_data = pat.sub(sub, data)
            if data != new_data:
                with path.open("w") as file:
                    file.write(new_data)
    
    root = pathlib.Path(sys.argv[1])
    
    for str_pat, sub in simple.items():
        replace_in_all_files(
            root,
            re.compile(rf"type\((.*?)\)\s*==\s*type\(\s*{str_pat}\s*\)"),
            rf"isinstance(\1, {sub})",
        )
        replace_in_all_files(
            root,
            re.compile(rf"type\((.*?)\)\s*!=\s*type\(\s*{str_pat}\s*\)"),
            rf"not isinstance(\1, {sub})",
        )
    
    for str_pat, sub in others.items():
        replace_in_all_files(root, re.compile(str_pat), sub)
    aqua-uru committed Mar 15, 2020
    Configuration menu
    Copy the full SHA
    7d91ef6 View commit details
    Browse the repository at this point in the history

Commits on Mar 17, 2020

  1. replace type and value check by "if x:"

    aqua-uru committed Mar 17, 2020
    Configuration menu
    Copy the full SHA
    1e707bb View commit details
    Browse the repository at this point in the history
  2. remove vault None checks

    aqua-uru committed Mar 17, 2020
    Configuration menu
    Copy the full SHA
    49b84ec View commit details
    Browse the repository at this point in the history
  3. replace plasma types in insinstance checks

    aqua-uru committed Mar 17, 2020
    Configuration menu
    Copy the full SHA
    b66e384 View commit details
    Browse the repository at this point in the history
  4. make some isinstance checks implicit

    aqua-uru committed Mar 17, 2020
    Configuration menu
    Copy the full SHA
    39f8de0 View commit details
    Browse the repository at this point in the history
  5. improve xPotsSymbol:OnFirstUpdate

    aqua-uru committed Mar 17, 2020
    Configuration menu
    Copy the full SHA
    c5ce19e View commit details
    Browse the repository at this point in the history
  6. change types module checks to isinstance

    This also helps with 2to3.
    aqua-uru committed Mar 17, 2020
    Configuration menu
    Copy the full SHA
    090897f View commit details
    Browse the repository at this point in the history