Fix bytes type in autogenerated code confusing pylance

This commit is contained in:
Lonami Exo 2024-03-16 13:48:11 +01:00
parent 9ede5bd3c5
commit 6fd3eb2ee6
2 changed files with 5 additions and 3 deletions

View File

@ -97,6 +97,7 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None:
writer.write("from typing import List, Optional, Self, Sequence")
writer.write("from .. import abcs")
writer.write("from ..core import Reader, Serializable, serialize_bytes_to")
writer.write("_bytes = bytes")
ns = f"{typedef.namespace[0]}." if typedef.namespace else ""
generated_type_names.add(f"{ns}{to_class_name(typedef.name)}")
@ -162,6 +163,7 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None:
writer.write("from typing import List, Optional, Self, Sequence")
writer.write("from .. import abcs")
writer.write("from ..core import Request, serialize_bytes_to")
writer.write("_bytes = bytes")
# def name(params, ...)
required_params = [p for p in functiondef.params if not is_computed(p.ty)]

View File

@ -65,7 +65,7 @@ _INNER_TYPE_MAP = {
"int128": "int",
"int256": "int",
"double": "float",
"bytes": "bytes",
"bytes": "_bytes", # parameters called "bytes" confuse the global bytes type
"string": "str",
}
@ -78,7 +78,7 @@ def inner_type_fmt(ty: Type) -> str:
elif ty.bare:
return to_class_name(ty.name)
elif ty.generic_ref:
return "bytes"
return "_bytes"
elif ty.name == "Object":
return "Serializable"
else:
@ -101,7 +101,7 @@ def param_type_fmt(ty: BaseParameter, *, immutable: bool) -> str:
else:
inner_ty = ty.ty
res = "bytes" if inner_ty.name == "Object" else inner_type_fmt(inner_ty)
res = "_bytes" if inner_ty.name == "Object" else inner_type_fmt(inner_ty)
if ty.ty.generic_arg:
if immutable: