-
Notifications
You must be signed in to change notification settings - Fork 34
replace type(x) == y
checks by isinstance(x, y)
and clean up resulting code
#120
Conversation
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like Cyan copy and pasted several anti-patterns that could be further cleaned up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like Cyan copy and pasted several anti-patterns that could be further cleaned up.
All comments addressed. Most of them were some easy regex but had to make sure they caught all your comments. |
return 0 | ||
else: | ||
PtDebugPrint("ERROR: clftYeeshaPage08: Error trying to access the Vault. Can't access YeeshaPageChanges chronicle." ) | ||
PtDebugPrint("ERROR: clftYeeshaPage08: Error trying to access the Chronicle psnlSDL. psnlSDL = %s" % ( psnlSDL)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ERROR: clftYeeshaPage08: Error trying to access the Chronicle psnlSDL. psnlSDL = None
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or at least psnlSDL
is falsey. It's like that on master. What do you think should happen here?
else: | ||
PtDebugPrint(u"xKI.GetYeeshaPageDefs(): Trying to access the Vault failed, can't access YeeshaPageChanges Chronicle.", level=kErrorLevel) | ||
PtDebugPrint(u"xKI.GetYeeshaPageDefs(): Trying to access the Chronicle psnlSDL failed: psnlSDL = \"{}\".".format(psnlSDL), level=kErrorLevel) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xKI.GetYeeshaPageDefs(): Trying to access the Chronicle psnlSDL failed: psnlSDL = None
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or at least psnlSDL
is falsey. It's like that on master. What do you think should happen here?
else: | ||
PtDebugPrint("xYeeshaPages: Error trying to access the Vault. Can't access YeeshaPageChanges chronicle." ) | ||
PtDebugPrint("xYeeshaPages: Error trying to access the Chronicle psnlSDL. psnlSDL = %s" % ( psnlSDL)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xYeeshaPages: Error trying to access the Chronicle psnlSDL. psnlSDL = None
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or at least psnlSDL
is falsey. It's like that on master. What do you think should happen here?
type(x) == type(y)
checks by isinstancetype(x) == type(y)
checks by isinstance and releated
type(x) == type(y)
checks by isinstance and releatedtype(x) == type(y)
checks by isinstance and related
type(x) == type(y)
checks by isinstance and relatedtype(x) == y
checks by isinstance(x, y)
and clean up resulting code
💥 💥 💥 |
Especially, replace things like
type(x) == type([])
byisinstance(x, list)
.Also do
x is None
special casing.Using the following py3 script: