Skip to content

Commit

Permalink
mark constants with no href with special type in stubs
Browse files Browse the repository at this point in the history
Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com>
  • Loading branch information
hoefling committed Aug 20, 2022
1 parent d5f5306 commit ca566bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
23 changes: 16 additions & 7 deletions src/xmlsec/constants.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ else:
from typing_extensions import Final

class __KeyData(NamedTuple): # __KeyData type
href: str | None
href: str
name: str

class __KeyDataNoHref(NamedTuple): # __KeyData type
href: None
name: str

class __Transform(NamedTuple): # __Transform type
href: str | None
href: str
name: str
usage: int

class __TransformNoHref(NamedTuple): # __Transform type
href: None
name: str
usage: int

Expand All @@ -32,9 +41,9 @@ KeyDataFormatPkcs8Der: Final[int]
KeyDataFormatPkcs8Pem: Final[int]
KeyDataFormatUnknown: Final[int]
KeyDataHmac: Final[__KeyData]
KeyDataName: Final[__KeyData]
KeyDataName: Final[__KeyDataNoHref]
KeyDataRawX509Cert: Final[__KeyData]
KeyDataRetrievalMethod: Final[__KeyData]
KeyDataRetrievalMethod: Final[__KeyDataNoHref]
KeyDataRsa: Final[__KeyData]
KeyDataTypeAny: Final[int]
KeyDataTypeNone: Final[int]
Expand All @@ -45,7 +54,7 @@ KeyDataTypeSession: Final[int]
KeyDataTypeSymmetric: Final[int]
KeyDataTypeTrusted: Final[int]
KeyDataTypeUnknown: Final[int]
KeyDataValue: Final[__KeyData]
KeyDataValue: Final[__KeyDataNoHref]
KeyDataX509: Final[__KeyData]
NodeCanonicalizationMethod: Final[str]
NodeCipherData: Final[str]
Expand Down Expand Up @@ -110,7 +119,7 @@ TransformKWAes192: Final[__Transform]
TransformKWAes256: Final[__Transform]
TransformKWDes3: Final[__Transform]
TransformMd5: Final[__Transform]
TransformRemoveXmlTagsC14N: Final[__Transform]
TransformRemoveXmlTagsC14N: Final[__TransformNoHref]
TransformRipemd160: Final[__Transform]
TransformRsaMd5: Final[__Transform]
TransformRsaOaep: Final[__Transform]
Expand All @@ -133,7 +142,7 @@ TransformUsageDigestMethod: Final[int]
TransformUsageEncryptionMethod: Final[int]
TransformUsageSignatureMethod: Final[int]
TransformUsageUnknown: Final[int]
TransformVisa3DHack: Final[__Transform]
TransformVisa3DHack: Final[__TransformNoHref]
TransformXPath: Final[__Transform]
TransformXPath2: Final[__Transform]
TransformXPointer: Final[__Transform]
Expand Down
17 changes: 13 additions & 4 deletions tests/test_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@
else:
from typing_extensions import Final
class __KeyData(NamedTuple): # __KeyData type
href: str | None
href: str
name: str
class __KeyDataNoHref(NamedTuple): # __KeyData type
href: None
name: str
class __Transform(NamedTuple): # __Transform type
href: str | None
href: str
name: str
usage: int
class __TransformNoHref(NamedTuple): # __Transform type
href: None
name: str
usage: int
"""

Expand All @@ -44,7 +50,10 @@ def gen_constants_stub():
def process_constant(name):
"""Generate line in stub file for constant name."""
obj = getattr(xmlsec.constants, name)
return '{name}: Final[{type_name}]'.format(name=name, type_name=type(obj).__name__)
type_name = type(obj).__name__
if type_name in ('__KeyData', '__Transform') and obj.href is None:
type_name += 'NoHref'
return '{name}: Final[{type_name}]'.format(name=name, type_name=type_name)

names = list(sorted(name for name in dir(xmlsec.constants) if not name.startswith('__')))
lines = [process_constant(name) for name in names]
Expand Down

0 comments on commit ca566bb

Please sign in to comment.