Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions jy/bridge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Python portals to JS"""

from typing import Any, Optional
from typing import Any
from functools import partial
from inspect import Parameter
from keyword import iskeyword
Expand Down Expand Up @@ -106,8 +106,8 @@ def add_js_funcs(
js_code: str,
*,
obj: Any = None,
name: Optional[str] = None,
encoding: Optional[str] = None,
name: str | None = None,
encoding: str | None = None,
forbidden_method_names=(),
apply_defaults=True,
value_trans=dflt_py_to_js_value_trans,
Expand Down
10 changes: 4 additions & 6 deletions jy/js_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from pathlib import Path
from functools import wraps
from itertools import chain
from typing import Optional
import json

import esprima
Expand Down Expand Up @@ -42,15 +41,15 @@ def _func(*args, **kwargs):
return _if_none_output_raise_wrapper


# def parse_js(js_code: str, encoding: Optional[str] = None) -> AstScript:
# def parse_js(js_code: str, encoding: str | None = None) -> AstScript:
# if os.path.isfile(js_code):
# js_code = Path(js_code).read_text(encoding=encoding)
# return esprima.parse(js_code)


def parse_js(
js_code: str,
encoding: Optional[str] = None,
encoding: str | None = None,
*,
ingress=lambda x: x,
egress=lambda x: x,
Expand Down Expand Up @@ -253,7 +252,6 @@ def dflt_py_to_js_value_trans(x):

import re
import uuid
from typing import Tuple

_dflt_patterns_for_html_ids = (
r'id="([^"]+)"', # HTML id attributes
Expand All @@ -263,8 +261,8 @@ def dflt_py_to_js_value_trans(x):


def replace_ids_in_code(
code: str, patterns: Tuple[str] = _dflt_patterns_for_html_ids
) -> (str, dict):
code: str, patterns: tuple[str] = _dflt_patterns_for_html_ids
) -> tuple[str, dict]:
"""
Replaces IDs in a mixed HTML, CSS, and JS string with unique versions.

Expand Down
6 changes: 3 additions & 3 deletions jy/scrap/find_and_replace_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import re
from functools import partial
from typing import Callable, Optional, Union
from collections.abc import Callable


def replace_tokens(string: str, token_extractor: Callable, token_replacer: Callable):
Expand Down Expand Up @@ -158,10 +158,10 @@ def _string_kind(string, string_kind_rules=_dflt_string_kind_rules):

def replace_ids(
string,
kind: Optional[str] = None,
kind: str | None = None,
*,
replacer_for_kind=_dflt_replacer_for_kind,
string_to_kind: Optional[Callable] = _string_kind
string_to_kind: Callable | None = _string_kind
):
kind = kind or string_to_kind(string)
replacer_for_kind = dict(replacer_for_kind)
Expand Down
25 changes: 13 additions & 12 deletions jy/ts_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ def parse_ts_with_oa(
# -------------------------------------------------------------------------------------
# The grammar-based-parsing way

from typing import Iterator, Dict, Any, Optional, Tuple
from typing import Any
from collections.abc import Iterator


def extract_parameters(code: str, parameters_node) -> Iterator[Dict[str, Any]]:
def extract_parameters(code: str, parameters_node) -> Iterator[dict[str, Any]]:
"""
Generator function that yields parameter dictionaries.
"""
if parameters_node:
for param_node in parameters_node.named_children:
param: Dict[str, Any] = {}
param: dict[str, Any] = {}
# Get parameter name
param_name_node = param_node.child_by_field_name("name")
if param_name_node:
Expand All @@ -130,7 +131,7 @@ def extract_parameters(code: str, parameters_node) -> Iterator[Dict[str, Any]]:
yield param # Yield the parameter


def find_function_type_node(node) -> Optional[Any]:
def find_function_type_node(node) -> Any | None:
"""
Recursively search for a 'function_type' node within a given node.
"""
Expand All @@ -143,7 +144,7 @@ def find_function_type_node(node) -> Optional[Any]:
return None


def handle_property_signature(code: str, node) -> Iterator[Tuple[str, Dict[str, Any]]]:
def handle_property_signature(code: str, node) -> Iterator[tuple[str, dict[str, Any]]]:
"""
Handle 'property_signature' nodes, yielding (property name, property info) tuples.
"""
Expand All @@ -152,7 +153,7 @@ def handle_property_signature(code: str, node) -> Iterator[Tuple[str, Dict[str,

if name_node:
prop_name = code[name_node.start_byte : name_node.end_byte]
prop_info: Dict[str, Any] = {"name": prop_name}
prop_info: dict[str, Any] = {"name": prop_name}

# Check if the property is optional (has a '?')
question_node = node.child_by_field_name("question_mark")
Expand All @@ -178,7 +179,7 @@ def handle_property_signature(code: str, node) -> Iterator[Tuple[str, Dict[str,
yield (prop_name, prop_info)


def handle_method_signature(code: str, node) -> Iterator[Tuple[str, Dict[str, Any]]]:
def handle_method_signature(code: str, node) -> Iterator[tuple[str, dict[str, Any]]]:
"""
Handle 'method_signature' nodes, yielding (method name, method info) tuples.
"""
Expand All @@ -188,7 +189,7 @@ def handle_method_signature(code: str, node) -> Iterator[Tuple[str, Dict[str, An

if name_node:
method_name = code[name_node.start_byte : name_node.end_byte]
method_info: Dict[str, Any] = {"name": method_name}
method_info: dict[str, Any] = {"name": method_name}

# Check if the method is optional (has a '?')
question_node = node.child_by_field_name("question_mark")
Expand All @@ -212,7 +213,7 @@ def handle_method_signature(code: str, node) -> Iterator[Tuple[str, Dict[str, An

def handle_interface_declaration(
code: str, node
) -> Iterator[Tuple[str, Dict[str, Any]]]:
) -> Iterator[tuple[str, dict[str, Any]]]:
"""
Handle 'interface_declaration' nodes by traversing their members.
"""
Expand All @@ -222,7 +223,7 @@ def handle_interface_declaration(
yield from parse_ts(code, member_node)


def default_handler(code: str, node) -> Iterator[Tuple[str, Dict[str, Any]]]:
def default_handler(code: str, node) -> Iterator[tuple[str, dict[str, Any]]]:
"""
Default handler for nodes that don't have a specific handler.
"""
Expand All @@ -232,7 +233,7 @@ def default_handler(code: str, node) -> Iterator[Tuple[str, Dict[str, Any]]]:


# Mapping from node types to handler functions
node_handlers: Dict[str, Any] = {
node_handlers: dict[str, Any] = {
"property_signature": handle_property_signature,
"method_signature": handle_method_signature,
"interface_declaration": handle_interface_declaration,
Expand All @@ -241,7 +242,7 @@ def default_handler(code: str, node) -> Iterator[Tuple[str, Dict[str, Any]]]:


# TODO: Add ability to parse patterns that the simple_ts_parser handles here too.
def parse_ts(code: str, node=None) -> Iterator[Tuple[str, Dict[str, Any]]]:
def parse_ts(code: str, node=None) -> Iterator[tuple[str, dict[str, Any]]]:
"""
Generator function that traverses the syntax tree and yields (name, info dict) tuples.

Expand Down
Loading
Loading