Skip to content

Commit

Permalink
stubber: createstubs - Fix Final.
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <Jos.Verlinde@microsoft.com>
  • Loading branch information
Josverl committed Aug 27, 2024
1 parent 402d949 commit e0bf224
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/stubber/board/createstubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,8 @@ def write_object_stub(

if t in ("str", "int", "float", "bool", "bytearray", "bytes"):
# known type: use actual value
# s = "{0}{1} = {2} # type: {3}\n".format(indent, item_name, item_repr, t)
if item_name.upper() == item_name:
s = "{0}{1}: {3} = Final[{2}]\n".format(indent, item_name, item_repr, t)
if item_name.upper() == item_name: # ALL_CAPS --> Final
s = "{0}{1}: Final[{3}] = {2}\n".format(indent, item_name, item_repr, t)
else:
s = "{0}{1}: {3} = {2}\n".format(indent, item_name, item_repr, t)
elif t in ("dict", "list", "tuple"):
Expand Down
5 changes: 2 additions & 3 deletions src/stubber/board/createstubs_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,8 @@ def write_object_stub(self, fp, object_expr: object, obj_name: str, indent: str,

if t in ("str", "int", "float", "bool", "bytearray", "bytes"):
# known type: use actual value
# s = "{0}{1} = {2} # type: {3}\n".format(indent, item_name, item_repr, t)
if item_name.upper() == item_name:
s = "{0}{1}: {3} = Final[{2}]\n".format(indent, item_name, item_repr, t)
if item_name.upper() == item_name: # ALL_CAPS --> Final
s = "{0}{1}: Final[{3}] = {2}\n".format(indent, item_name, item_repr, t)
else:
s = "{0}{1}: {3} = {2}\n".format(indent, item_name, item_repr, t)
elif t in ("dict", "list", "tuple"):
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/board/createstubs_db_min.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def write_object_stub(L,fp,object_expr,obj_name,indent,in_class=0):
elif I.startswith("<class '"):
G=I[8:-2];D=B
if G in(x,v,w,y,'bytearray','bytes'):
if C.upper()==C:D='{0}{1}: {3} = Final[{2}]\n'.format(E,C,H,G)
if C.upper()==C:D='{0}{1}: Final[{3}] = {2}\n'.format(E,C,H,G)
else:D=X.format(E,C,H,G)
elif G in(g,f,e):c={g:'{}',f:'[]',e:'()'};D=X.format(E,C,c[G],G)
elif G in('object','set','frozenset','Pin',Y):
Expand Down
Binary file modified src/stubber/board/createstubs_db_mpy.mpy
Binary file not shown.
5 changes: 2 additions & 3 deletions src/stubber/board/createstubs_mem.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,8 @@ def write_object_stub(self, fp, object_expr: object, obj_name: str, indent: str,

if t in ("str", "int", "float", "bool", "bytearray", "bytes"):
# known type: use actual value
# s = "{0}{1} = {2} # type: {3}\n".format(indent, item_name, item_repr, t)
if item_name.upper() == item_name:
s = "{0}{1}: {3} = Final[{2}]\n".format(indent, item_name, item_repr, t)
if item_name.upper() == item_name: # ALL_CAPS --> Final
s = "{0}{1}: Final[{3}] = {2}\n".format(indent, item_name, item_repr, t)
else:
s = "{0}{1}: {3} = {2}\n".format(indent, item_name, item_repr, t)
elif t in ("dict", "list", "tuple"):
Expand Down
2 changes: 1 addition & 1 deletion src/stubber/board/createstubs_mem_min.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def write_object_stub(L,fp,object_expr,obj_name,indent,in_class=0):
elif I.startswith("<class '"):
G=I[8:-2];D=B
if G in(r,p,q,s,'bytearray','bytes'):
if C.upper()==C:D='{0}{1}: {3} = Final[{2}]\n'.format(F,C,H,G)
if C.upper()==C:D='{0}{1}: Final[{3}] = {2}\n'.format(F,C,H,G)
else:D=X.format(F,C,H,G)
elif G in(e,d,c):f={e:'{}',d:'[]',c:'()'};D=X.format(F,C,f[G],G)
elif G in('object','set','frozenset','Pin',Y):
Expand Down
Binary file modified src/stubber/board/createstubs_mem_mpy.mpy
Binary file not shown.
2 changes: 1 addition & 1 deletion src/stubber/board/createstubs_min.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def write_object_stub(L,fp,object_expr,obj_name,indent,in_class=0):
elif I.startswith("<class '"):
G=I[8:-2];D=B
if G in(r,p,q,s,'bytearray','bytes'):
if C.upper()==C:D='{0}{1}: {3} = Final[{2}]\n'.format(E,C,H,G)
if C.upper()==C:D='{0}{1}: Final[{3}] = {2}\n'.format(E,C,H,G)
else:D=X.format(E,C,H,G)
elif G in(e,d,c):f={e:'{}',d:'[]',c:'()'};D=X.format(E,C,f[G],G)
elif G in('object','set','frozenset','Pin',Y):
Expand Down
Binary file modified src/stubber/board/createstubs_mpy.mpy
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/createstubs/literal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_literal_init_order(
# check literals defined before first method

class_line = lines.index("class SoftSPI():")
LSB_line = lines.index(" LSB: int = 1", class_line + 1)
LSB_line = lines.index(" LSB: Final[int] = 1", class_line + 1)
init_line = lines.index(" def __init__(self, *argv, **kwargs) -> None:", class_line + 1)

assert class_line < LSB_line < init_line, "Literals MUST be listed before class methods"

0 comments on commit e0bf224

Please sign in to comment.