Replies: 1 comment
-
Thanks Joseph for investigating this. I don't think there is a need for this level of planning here. Like other modern python features, we can adopt this as new code is written or existing code is modified. I don't think it is a good use of time to compare performances, survey code or perform extensive testing. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
Now that Python 3.11 powers NVDA, it would be great to modernize NVDA source code, and one such possibility is below. Note that an issue is not used as this should serve as a starting point with issues to cover individual or hard cases.
Introduction
Python 3.10 introduced structural pattern matching (match/case). This allows Python code to use a more declarative syntax when handling conditions and other scenarios.
A typical structural pattern matching syntax is:
subject = someExpression/function()
match subject:
case case1:
doSomething
case case2:
doSomethingElse()
case "case3A" | "case3B":
performSomethingThatTakesOneOrBothMatches()
case ["case4", someValue]:
complicatedProcedureInvolvingMatchAndSomeValue()
case ["case5A", "case5B"] as something:
quiteAComplicatedProcedure()
case _:
everythingElse
For example, if NVDA wants to insert specific overlay class based on window class name:
windowClassName = obj.windowClassName
match windowClassName: # (or you can write match obj.windowClassName but this one can get verbose)
case "class1":
clsList.insert(0, OverlayClass1)
case "class2":
clsList.insert(0, OverlayClass2)
case "class3A" | "class3B":
clsList.remove(UnwantedOverlayClass)
Here, "case _:" is omitted.
You can also do something similar to the following (example: event_NVDAObject_init):
match obj.role:
case controlTypes.Role.CHECKBOX:
obj.name = aMoreUsefulName
case controlTypes.Role.STATICTEXT | controlTypes.Role.PANE:
if obj.windowClassName.endswith("form"):
obj.description = ""
Structural pattern matching and NVDA source code
The following places in NVDA source code may see improved readability when structural pattern matching is employed:
Places where structural pattern maching is not useful:
NVDA and other tools
Flake8 4.0.1 supports structural pattern matching so we can use match/case statements right away.
Impact on add-ons
Add-ons wishing to employ structural pattern matching must do one of the following:
Positions
Reasons to adopt structural pattern matching:
Drawbacks:
If structural pattern matching is adopted
If we say "yes" to structural pattern matching:
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions