Skip to content

Commit

Permalink
Fix type issues with lua parser
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCat467 committed Feb 2, 2024
1 parent bdc4abe commit 00c2f73
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/market_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class SearchPublication(NamedTuple):

async def get_publications(
client: httpx.AsyncClient,
category_id: PUBLICATION_CATEGORY = None,
category_id: PUBLICATION_CATEGORY | None = None,
offset: int | None = None,
count: int = 100,
search: str | None = None,
Expand Down
34 changes: 19 additions & 15 deletions src/market_api/lua_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def __init__(
self,
name: str,
*args: T,
):
) -> None:
"""Set up name and arguments."""
self.name = name
if args:
Expand Down Expand Up @@ -418,26 +418,26 @@ def parse_numeric_literal(self) -> Value[int | float]:
value = float(text) if is_float else int(text)
return Value("Float" if is_float else "Integer", value)

def parse_field(self) -> Value[str | Value[object]]:
def parse_field(self) -> Value[Any]:
"""Parse table field."""
if self.lookup() == "[":
self.expect("[")
value = self.parse_value()
self.expect("]")
self.expect_type(Assignment)
return Value("Field", value, self.parse_value()) # type: ignore[return-value]
return Value("Field", value, self.parse_value())
if isinstance(self.peek(), Identifier):
return self.parse_identifier() # type: ignore[return-value]
return self.parse_identifier()
index = self.next_indexed_field.pop()
self.next_indexed_field.append(index + 1)
# return Value("Indexed", self.parse_value())
return Value("Field", Value("Integer", index), self.parse_value()) # type: ignore[return-value]
return Value("Field", Value("Integer", index), self.parse_value())

def parse_table(self) -> Value[object]:
def parse_table(self) -> Value[Value[Any]]:
"""Parse table."""
self.expect("{")
self.next_indexed_field.append(1)
fields = []
fields: list[Value[object | Value[object]]] = []
while self.lookup() != "}":
fields.append(self.parse_field())

Expand All @@ -447,7 +447,7 @@ def parse_table(self) -> Value[object]:
self.expect("}")
return Value("Table", *fields)

def parse_function_arguments(self) -> list[Value[object]]:
def parse_function_arguments(self) -> list[Value[Any]]:
"""Parse function call arguments."""
self.expect("(")
arguments = []
Expand All @@ -459,7 +459,7 @@ def parse_function_arguments(self) -> list[Value[object]]:
self.expect(")")
return arguments

def parse_identifier(self) -> Value[object]:
def parse_identifier(self) -> Value[Any]:
"""Parse identifier."""
identifier = self.expect_type(Identifier)
text = identifier.text
Expand All @@ -478,7 +478,7 @@ def parse_identifier(self) -> Value[object]:
return Value(
"FunctionCall",
Value("Identifier", text),
Value("Arguments", *self.parse_table()),
Value("Arguments", *self.parse_table()), # type: ignore[misc]
)
if isinstance(
self.peek(),
Expand All @@ -487,7 +487,7 @@ def parse_identifier(self) -> Value[object]:
return Value(
"FunctionCall",
Value("Identifier", text),
Value("Arguments", *self.parse_string_literal()),
Value("Arguments", *self.parse_string_literal()), # type: ignore[misc]
)
if isinstance(self.peek(), Assignment):
self.expect_type(Assignment)
Expand Down Expand Up @@ -519,7 +519,7 @@ def parse_lua_table(text: str, convert_lists: bool = True) -> object:
value = parser.parse_value()
# print(value)

def read_value(value: Value[str | Value[object]]) -> object:
def read_value(value: Value[object]) -> object:
"""Read value base function."""
assert isinstance(value, Value)
if value.name in {
Expand All @@ -537,12 +537,12 @@ def read_value(value: Value[str | Value[object]]) -> object:
raise NotImplementedError(value.name)

def read_assignment(
value: Value[Value[str | Value[object]]],
value: Value[Any],
) -> tuple[str, object]:
"""Read an Assignment value."""
assert value.name == "Assignment"
key, data = value.args
return (read_value(key), read_value(data))
return (read_value(key), read_value(data)) # type: ignore[return-value]

def read_field(
value: Value[object],
Expand All @@ -556,7 +556,11 @@ def read_field(
# return (str(len(table)), read_value(value.args[0]))
if value.name == "Field":
field, field_value = value.args
return (read_value(field), read_value(field_value))
assert isinstance(field, Value)
assert isinstance(field_value, Value)
read_field_obj = read_value(field)
assert isinstance(read_field_obj, str | int)
return (read_field_obj, read_value(field_value))
raise NotImplementedError(value.name)

def read_table(
Expand Down

0 comments on commit 00c2f73

Please sign in to comment.