Skip to content

Commit cbe88df

Browse files
authored
Merge pull request #302 from zhangxichang/chore/add-basedpyright-and-refactor
chore: add basedpyright, refactor android sign config with better type safety
2 parents 8e76636 + 7654f60 commit cbe88df

4 files changed

Lines changed: 75 additions & 27 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"check": "run-p 'check:*'",
1818
"check:tsc": "tsc",
1919
"check:eslint": "eslint src",
20-
"check:format": "biome check",
20+
"check:script": "uv run basedpyright scripts",
2121
"native:dev": "tauri dev",
2222
"native:build": "tauri build",
2323
"native:build:debug": "tauri build --debug",

pyproject.toml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
name = "scripts"
33
version = "0.1.0"
44
requires-python = ">=3.14"
5-
dependencies = [
5+
6+
dependencies = ["kopyt>=0.0.2"]
7+
8+
[dependency-groups]
9+
lint = ["basedpyright>=1.39.3"]
10+
jupyter = [
611
"black>=26.3.1",
712
"isort>=8.0.1",
813
"jupyterlab>=4.5.7",
914
"jupyterlab-code-formatter>=3.0.3",
10-
"jupyterlab-lsp>=5.2.0",
11-
"kopyt>=0.0.2",
12-
"python-lsp-server[all]>=1.14.0",
15+
"jupyterlab-lsp>=5.3.0",
16+
"python-lsp-server>=1.14.0",
1317
]
18+
19+
[tool.pyright]
20+
typeCheckingMode = "standard"

scripts/insert_android_sign_config.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from argparse import ArgumentParser
22
from os import path
33
from shutil import copyfile
4-
from typing import Optional
4+
from typing import Optional, TypeGuard
55
from kopyt import Parser
66
from kopyt.node import (
77
CallSuffix,
@@ -14,19 +14,21 @@
1414
from kopyt.position import Position
1515

1616

17-
def is_postfix_unary_expression(node) -> bool:
17+
def is_postfix_unary_expression(node) -> TypeGuard[PostfixUnaryExpression]:
1818
return isinstance(node, PostfixUnaryExpression)
1919

2020

21-
def is_simple_identifier(node, value: Optional[str] = None) -> bool:
21+
def is_simple_identifier(
22+
node, value: Optional[str] = None
23+
) -> TypeGuard[SimpleIdentifier]:
2224
if not isinstance(node, SimpleIdentifier):
2325
return False
2426
if value is not None and node.value != value:
2527
return False
2628
return True
2729

2830

29-
def is_call_suffix(node) -> bool:
31+
def is_call_suffix(node) -> TypeGuard[CallSuffix]:
3032
return isinstance(node, CallSuffix)
3133

3234

@@ -36,6 +38,18 @@ def get_call_suffix_with_lambda(node) -> Optional[CallSuffix]:
3638
return None
3739

3840

41+
def _has_release_value_arg(arguments) -> bool:
42+
"""Check if any ValueArgument wraps a LineStringLiteral '"release"'."""
43+
for arg in arguments:
44+
if not isinstance(arg, ValueArgument):
45+
continue
46+
if not isinstance(arg.value, LineStringLiteral):
47+
continue
48+
if arg.value.value == '"release"':
49+
return True
50+
return False
51+
52+
3953
def find_suffixes_with_lambda(postfix_node: PostfixUnaryExpression) -> list[CallSuffix]:
4054
result = []
4155
for suffix in postfix_node.suffixes:
@@ -68,14 +82,8 @@ def find_call_suffix_with_release_arg(
6882
continue
6983
if suffix.arguments is None:
7084
continue
71-
72-
for arg in suffix.arguments:
73-
if not isinstance(arg, ValueArgument):
74-
continue
75-
if not isinstance(arg.value, LineStringLiteral):
76-
continue
77-
if arg.value.value == '"release"':
78-
return suffix
85+
if _has_release_value_arg(suffix.arguments):
86+
return suffix
7987

8088
return None
8189

@@ -87,6 +95,7 @@ def find_release_buildtype_suffix(
8795
call_suffix = get_call_suffix_with_lambda(buildtypes_suffix)
8896
if not call_suffix:
8997
continue
98+
assert call_suffix.lambda_expression is not None
9099

91100
buildtypes_lambda = call_suffix.lambda_expression.value
92101
buildtypes_expr = find_expression_by_name(
@@ -138,10 +147,10 @@ def find_release_buildtype_suffix(
138147
use_signing_configs_code = 'signingConfig = signingConfigs.getByName("release")'
139148

140149
for root_statement in gradle_build_script.statements:
141-
if not is_postfix_unary_expression(root_statement.statement):
150+
android_expr = root_statement.statement
151+
if not is_postfix_unary_expression(android_expr):
142152
continue
143153

144-
android_expr = root_statement.statement
145154
if not is_simple_identifier(android_expr.expression, "android"):
146155
continue
147156

@@ -150,11 +159,13 @@ def find_release_buildtype_suffix(
150159
continue
151160

152161
android_call = call_suffixes[0]
153-
android_lambda = android_call.lambda_expression.value
162+
android_lambda = android_call.lambda_expression
163+
if android_lambda is None:
164+
continue
154165

155-
android_lambda.statements = [Parser(signing_configs_code).parse_statement()] + list(
156-
android_lambda.statements
157-
)
166+
android_lambda.value.statements = [
167+
Parser(signing_configs_code).parse_statement()
168+
] + list(android_lambda.value.statements)
158169

159170
release_suffix = find_release_buildtype_suffix(android_expr)
160171
if release_suffix and release_suffix.lambda_expression:

uv.lock

Lines changed: 34 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)