added stub for Mapping

enhances stubs for Struct, Sequence
This commit is contained in:
Tim Rid 2020-12-17 19:56:10 +01:00
parent f472732cd6
commit 379e2010c2
3 changed files with 13 additions and 20 deletions

View file

@ -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",

View file

@ -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:

View file

@ -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: ...