From 379e2010c2efad3e82fe295573838b3694f3a74f Mon Sep 17 00:00:00 2001 From: Tim Rid <6593626+timrid@users.noreply.github.com> Date: Thu, 17 Dec 2020 19:56:10 +0100 Subject: [PATCH] added stub for Mapping enhances stubs for Struct, Sequence --- .vscode/settings.json | 6 +++--- README.md | 10 ++++------ construct-stubs/core.pyi | 17 ++++++----------- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9c8fcc8..f7ae8a2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,8 @@ { "python.languageServer": "Pylance", - "python.testing.unittestEnabled": false, - "python.testing.nosetestsEnabled": false, - "python.testing.pytestEnabled": true, + // "python.testing.unittestEnabled": false, + // "python.testing.nosetestsEnabled": false, + // "python.testing.pytestEnabled": true, "pythonTestExplorer.testFramework": "pytest", "python.formatting.provider": "black", "python.analysis.typeCheckingMode": "strict", diff --git a/README.md b/README.md index 39826a9..413d53f 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,10 @@ For example: - an `Array(5, Int16ub)` construct parses to a `ListContainer[int]` and can be build from an `List[int]`. The problem is to describe the more complex constructs like: - - `Sequence` which has heterogenous subcons in comparison to an `Array` with only homogenous subcons. - - `Struct`, `BitStruct`, `Union` which has heterogenous and named subcons. - -Currently only the very unspecific type `typing.Any` can be used as type hint (maybe in the future it can be optimised a little, when variadic generics become available). But the biggest disadvantage is that autocompletion for the named subcons is not available. + - `Sequence`, `FocusedSeq` which has heterogenous subcons in comparison to an `Array` with only homogenous subcons. + - `Struct`, `BitStruct`, `LazyStruct`, `Union` which has heterogenous and named subcons. +Currently only the very unspecific type `typing.Any` can be used as type hint (maybe in the future it can be optimised a little, when [variadic generics](https://mail.python.org/archives/list/typing-sig@python.org/thread/SQVTQYWIOI4TIO7NNBTFFWFMSMS2TA4J/) become available). But the biggest disadvantage is that autocompletion for the named subcons is not available. ### Typed To include autocompletion and further enhance the type hints for these complex constructs the **construct_typed** package is used as an extension to the original *construct* package. @@ -63,5 +62,4 @@ print(format.build(obj)) print(format.parse(b"BMP\x03\x02\x07\x08\t\x0b\x0c\r")) ``` -An example of the added `TypedEnum` class: - +An example of the added `TypedEnum` class: \ No newline at end of file diff --git a/construct-stubs/core.pyi b/construct-stubs/core.pyi index 23dcbb7..3c62941 100644 --- a/construct-stubs/core.pyi +++ b/construct-stubs/core.pyi @@ -283,12 +283,14 @@ class FlagsEnum(Adapter[int, int, Container[bool], t.Union[int, str, t.Dict[str, ) -> None: ... def __getattr__(self, name: str) -> BitwisableString: ... +class Mapping(Adapter[SubconParsedType, SubconBuildTypes, t.Any, t.Any]): + def __init__(self, subcon: Construct[SubconParsedType, SubconBuildTypes], mapping: t.Dict[t.Any, t.Any]) -> None: ... # =============================================================================== # structures and sequences # =============================================================================== # this can maybe made better when variadic generics are available -class Struct(Construct[Container[t.Any], t.Dict[str, t.Any]]): +class Struct(Construct[Container[t.Any], t.Optional[t.Dict[str, t.Any]]]): def __init__( self, *subcons: Construct[t.Any, t.Any], @@ -296,16 +298,9 @@ class Struct(Construct[Container[t.Any], t.Dict[str, t.Any]]): ) -> None: ... # this can maybe made better when variadic generics are available -class Sequence(Construct[ListContainer[ParsedType], t.List[BuildTypes]]): - @t.overload +class Sequence(Construct[ListContainer[t.Any], t.Optional[t.List[t.Any]]]): def __init__( - self: Sequence[ParsedType, BuildTypes], - *subcons: Construct[ParsedType, BuildTypes], - **subconskw: Construct[ParsedType, BuildTypes] - ) -> None: ... - @t.overload - def __init__( - self: Sequence[t.Any, t.Any], + self, *subcons: Construct[t.Any, t.Any], **subconskw: Construct[t.Any, t.Any] ) -> None: ... @@ -331,7 +326,7 @@ class GreedyRange(Subconstruct[SubconParsedType, SubconBuildTypes, ListContainer class RepeatUntil(Subconstruct[SubconParsedType, SubconBuildTypes, ListContainer[SubconParsedType], t.List[SubconBuildTypes]]): def __init__( self, - predicate: t.Union[bool, t.Callable[[Construct[SubconParsedType, SubconBuildTypes], ListContainer[SubconParsedType], Context], bool]], + predicate: t.Union[bool, t.Callable[[SubconParsedType, ListContainer[SubconParsedType], Context], bool]], subcon: Construct[SubconParsedType, SubconBuildTypes], discard: bool = ... ) -> None: ...