Skip to content

Commit 7ef6187

Browse files
authored
[vobject] Improve stubs (#14299)
1 parent cafde84 commit 7ef6187

File tree

8 files changed

+173
-131
lines changed

8 files changed

+173
-131
lines changed

stubs/vobject/@tests/stubtest_allowlist.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ vobject.win32tz
99
# python2 compat
1010
vobject.base.basestring
1111
vobject.base.str_
12-
vobject.base.to_unicode
13-
vobject.base.to_basestring
1412
vobject.base.unicode_type
1513

1614
# implementation details that users shouldn't depend on

stubs/vobject/vobject/__init__.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from .base import VERSION as VERSION, Component
2-
3-
__version__ = VERSION
1+
from . import icalendar as icalendar, vcard as vcard
2+
from .base import VERSION as VERSION, Component, readComponents as readComponents, readOne as readOne
43

54
def iCalendar() -> Component: ...
65
def vCard() -> Component: ...

stubs/vobject/vobject/base.pyi

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import logging
2-
from _typeshed import Incomplete, SupportsWrite
3-
from collections.abc import Iterable, Iterator
4-
from typing import Any, Final, Literal, TypeVar, overload
2+
import re
3+
from _typeshed import Incomplete, MaybeNone, SupportsWrite
4+
from collections.abc import Generator, Iterator
5+
from typing import Any, AnyStr, Final, Literal, TypeVar, overload
6+
from typing_extensions import Self
57

68
_V = TypeVar("_V", bound=VBase)
79
_W = TypeVar("_W", bound=SupportsWrite[bytes])
810

11+
VERSION: Final[str]
12+
13+
def to_unicode(value: str | bytes | bytearray) -> str: ...
14+
def to_basestring(s: str | bytes) -> bytes: ...
15+
916
logger: logging.Logger
1017
DEBUG: bool
1118
CR: str
@@ -15,8 +22,6 @@ SPACE: str
1522
TAB: str
1623
SPACEORTAB: str
1724

18-
VERSION: Final[str]
19-
2025
class VBase:
2126
group: Incomplete | None
2227
behavior: Incomplete | None
@@ -41,44 +46,53 @@ class VBase:
4146
@overload
4247
def serialize(self, buf: _W, lineLength: int = 75, validate: bool = True, behavior=None, *args: Any, **kwargs: Any) -> _W: ...
4348

44-
def toVName(name, stripNum: int = 0, upper: bool = False): ...
49+
def toVName(name: str, stripNum: int = 0, upper: bool = False) -> str: ...
4550

4651
class ContentLine(VBase):
47-
name: Incomplete
48-
encoded: Incomplete
49-
params: Incomplete
50-
singletonparams: Incomplete
51-
isNative: Incomplete
52-
lineNumber: Incomplete
53-
value: Incomplete
52+
name: str
53+
encoded: bool
54+
params: dict[Incomplete, list[Incomplete]]
55+
singletonparams: list[Incomplete]
56+
isNative: bool
57+
lineNumber: int | None
58+
value: str
5459
def __init__(
55-
self, name, params, value, group=None, encoded: bool = False, isNative: bool = False, lineNumber=None, *args, **kwds
60+
self,
61+
name: str,
62+
params: dict[Incomplete, list[Incomplete]],
63+
value: str,
64+
group=None,
65+
encoded: bool = False,
66+
isNative: bool = False,
67+
lineNumber: int | None = None,
68+
*args,
69+
**kwds,
5670
) -> None: ...
5771
@classmethod
58-
def duplicate(cls, copyit): ...
72+
def duplicate(cls, copyit) -> Self: ...
5973
def copy(self, copyit) -> None: ...
6074
def __eq__(self, other): ...
6175
def __getattr__(self, name: str): ...
6276
def __setattr__(self, name: str, value) -> None: ...
6377
def __delattr__(self, name: str) -> None: ...
64-
def valueRepr(self): ...
78+
def valueRepr(self) -> str: ...
6579
def __unicode__(self) -> str: ...
6680
def prettyPrint(self, level: int = 0, tabwidth: int = 3) -> None: ...
6781

6882
class Component(VBase):
6983
contents: dict[str, list[VBase]]
70-
name: Incomplete
84+
name: str
7185
useBegin: bool
72-
def __init__(self, name=None, *args, **kwds) -> None: ...
86+
def __init__(self, name: str | None = None, *args, **kwds) -> None: ...
7387
@classmethod
74-
def duplicate(cls, copyit): ...
88+
def duplicate(cls, copyit) -> Self: ...
7589
def copy(self, copyit) -> None: ...
76-
def setProfile(self, name) -> None: ...
90+
def setProfile(self, name: str) -> None: ...
7791
def __getattr__(self, name: str): ...
78-
normal_attributes: Incomplete
92+
normal_attributes: list[str]
7993
def __setattr__(self, name: str, value) -> None: ...
8094
def __delattr__(self, name: str) -> None: ...
81-
def getChildValue(self, childName, default=None, childNumber: int = 0): ...
95+
def getChildValue(self, childName: str, default=None, childNumber: int = 0): ...
8296
@overload
8397
def add(self, objOrName: _V, group: str | None = None) -> _V: ...
8498
@overload
@@ -91,45 +105,47 @@ class Component(VBase):
91105
def add(self, objOrName: str, group: str | None = None) -> Any: ... # returns VBase sub-class
92106
def remove(self, obj) -> None: ...
93107
def getChildren(self) -> list[Incomplete]: ...
94-
def components(self) -> Iterable[Component]: ...
95-
def lines(self): ...
96-
def sortChildKeys(self): ...
97-
def getSortedChildren(self): ...
108+
def components(self) -> Generator[Component]: ...
109+
def lines(self) -> Generator[ContentLine]: ...
110+
def sortChildKeys(self) -> list[Incomplete]: ...
111+
def getSortedChildren(self) -> list[Incomplete]: ...
98112
def setBehaviorFromVersionLine(self, versionLine) -> None: ...
99113
def transformChildrenToNative(self) -> None: ...
100114
def transformChildrenFromNative(self, clearBehavior: bool = True) -> None: ...
101115
def prettyPrint(self, level: int = 0, tabwidth: int = 3) -> None: ...
102116

103117
class VObjectError(Exception):
104-
msg: Incomplete
105-
lineNumber: Incomplete
106-
def __init__(self, msg, lineNumber=None) -> None: ...
118+
msg: str
119+
lineNumber: int
120+
def __init__(self, msg: str, lineNumber: int | None = None) -> None: ...
107121

108122
class ParseError(VObjectError): ...
109123
class ValidateError(VObjectError): ...
110124
class NativeError(VObjectError): ...
111125

112-
patterns: Incomplete
113-
param_values_re: Incomplete
114-
params_re: Incomplete
115-
line_re: Incomplete
116-
begin_re: Incomplete
126+
patterns: dict[str, str]
127+
param_values_re: re.Pattern[str]
128+
params_re: re.Pattern[str]
129+
line_re: re.Pattern[str]
130+
begin_re: re.Pattern[str]
117131

118-
def parseParams(string): ...
119-
def parseLine(line, lineNumber=None): ...
132+
def parseParams(string: str) -> list[list[Any]]: ... # Any was taken from re module stubs
133+
def parseLine(
134+
line: str, lineNumber: int | None = None
135+
) -> tuple[str, list[list[Any]], str | MaybeNone, str | MaybeNone]: ... # Any is result of parseParams()
120136

121-
wrap_re: Incomplete
122-
logical_lines_re: Incomplete
137+
wrap_re: re.Pattern[str]
138+
logical_lines_re: re.Pattern[str]
123139
testLines: str
124140

125-
def getLogicalLines(fp, allowQP: bool = True) -> None: ...
126-
def textLineToContentLine(text, n=None): ...
127-
def dquoteEscape(param): ...
128-
def foldOneLine(outbuf, input, lineLength: int = 75) -> None: ...
129-
def defaultSerialize(obj, buf, lineLength): ...
141+
def getLogicalLines(fp, allowQP: bool = True) -> Generator[tuple[str, int]]: ...
142+
def textLineToContentLine(text, n: int | None = None) -> ContentLine: ...
143+
def dquoteEscape(param: str) -> str: ...
144+
def foldOneLine(outbuf: SupportsWrite[AnyStr], input: AnyStr, lineLength: int = 75) -> None: ...
145+
def defaultSerialize(obj: Component | ContentLine, buf, lineLength: int): ...
130146

131147
class Stack:
132-
stack: Incomplete
148+
stack: list[Incomplete]
133149
def __len__(self) -> int: ...
134150
def top(self): ...
135151
def topName(self): ...
@@ -141,7 +157,7 @@ def readComponents(
141157
streamOrString, validate: bool = False, transform: bool = True, ignoreUnreadable: bool = False, allowQP: bool = False
142158
) -> Iterator[Component]: ...
143159
def readOne(stream, validate: bool = False, transform: bool = True, ignoreUnreadable: bool = False, allowQP: bool = False): ...
144-
def registerBehavior(behavior, name=None, default: bool = False, id=None) -> None: ...
145-
def getBehavior(name, id=None): ...
146-
def newFromBehavior(name, id=None): ...
147-
def backslashEscape(s): ...
160+
def registerBehavior(behavior, name: str | None = None, default: bool = False, id=None) -> None: ...
161+
def getBehavior(name: str, id=None): ...
162+
def newFromBehavior(name: str, id=None) -> Component | ContentLine: ...
163+
def backslashEscape(s: str) -> str: ...

stubs/vobject/vobject/change_tz.pyi

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
import datetime
2+
import optparse
13
from collections.abc import Sequence
4+
from typing import Final, Literal
25

3-
def change_tz(cal, new_timezone, default, utc_only: bool = False, utc_tz=...) -> None: ...
6+
from .base import Component
7+
8+
version: Final[str]
9+
10+
def change_tz(
11+
cal: Component,
12+
new_timezone: datetime._TzInfo | None,
13+
default: datetime._TzInfo | None,
14+
utc_only: bool = False,
15+
utc_tz: datetime._TzInfo | None = ...,
16+
) -> None: ...
417
def show_timezones() -> None: ...
518
def convert_events(utc_only: bool, args: Sequence[str]) -> None: ...
619
def main() -> None: ...
7-
8-
version: str
9-
10-
def get_options(): ...
20+
def get_options() -> tuple[optparse.Values, Literal[False]] | tuple[optparse.Values, list[str]]: ...

stubs/vobject/vobject/icalendar.pyi

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import datetime
22
from _typeshed import Incomplete
3-
from datetime import timedelta
4-
from typing import Any, Final
3+
from collections.abc import Iterable
4+
from datetime import timedelta, tzinfo
5+
from typing import Final, Literal, overload
56

67
from .base import Component
78
from .behavior import Behavior
@@ -15,11 +16,14 @@ FREQUENCIES: Final[tuple[str, ...]]
1516
ZERO_DELTA: Final[timedelta]
1617
twoHours: Final[timedelta]
1718

19+
@overload
20+
def toUnicode(s: None) -> None: ...
21+
@overload
1822
def toUnicode(s: str | bytes) -> str: ...
19-
def registerTzid(tzid, tzinfo) -> None: ...
20-
def getTzid(tzid, smart: bool = True): ...
23+
def registerTzid(tzid: str | bytes, tzinfo) -> None: ...
24+
def getTzid(tzid: str, smart: bool = True): ...
2125

22-
utc: Any # dateutil.tz.tz.tzutc
26+
utc: tzinfo # dateutil.tz.tz.tzutc (subclass of tzinfo)
2327

2428
class TimezoneComponent(Component):
2529
isNative: bool
@@ -29,16 +33,16 @@ class TimezoneComponent(Component):
2933
useBegin: bool
3034
def __init__(self, tzinfo=None, *args, **kwds) -> None: ...
3135
@classmethod
32-
def registerTzinfo(cls, tzinfo): ...
36+
def registerTzinfo(cls, tzinfo) -> str | None: ...
3337
def gettzinfo(self): ...
3438
tzid: Incomplete
3539
daylight: Incomplete
3640
standard: Incomplete
37-
def settzinfo(self, tzinfo, start: int = 2000, end: int = 2030): ...
41+
def settzinfo(self, tzinfo, start: int = 2000, end: int = 2030) -> None: ...
3842
normal_attributes: Incomplete
3943
@staticmethod
40-
def pickTzid(tzinfo, allowUTC: bool = False): ...
41-
def prettyPrint(self, level, tabwidth) -> None: ... # type: ignore[override]
44+
def pickTzid(tzinfo, allowUTC: bool = False) -> str | None: ...
45+
def prettyPrint(self, level: int, tabwidth: int) -> None: ... # type: ignore[override]
4246

4347
class RecurringComponent(Component):
4448
isNative: bool
@@ -203,26 +207,30 @@ class FreeBusy(PeriodBehavior):
203207

204208
class RRule(Behavior): ...
205209

206-
utcDateTimeList: Incomplete
207-
dateTimeOrDateList: Incomplete
208-
textList: Incomplete
210+
utcDateTimeList: list[str]
211+
dateTimeOrDateList: list[str]
212+
textList: list[str]
209213

210-
def numToDigits(num, places): ...
211-
def timedeltaToString(delta): ...
212-
def timeToString(dateOrDateTime): ...
213-
def dateToString(date): ...
214-
def dateTimeToString(dateTime, convertToUTC: bool = False): ...
215-
def deltaToOffset(delta): ...
214+
def numToDigits(num: float | None, places: int) -> str: ...
215+
def timedeltaToString(delta: datetime.timedelta) -> str: ...
216+
def timeToString(dateOrDateTime: datetime.date | datetime.datetime) -> str: ...
217+
def dateToString(date: datetime.date) -> str: ...
218+
def dateTimeToString(dateTime: datetime.datetime, convertToUTC: bool = False) -> str: ...
219+
def deltaToOffset(delta: datetime.timedelta) -> str: ...
216220
def periodToString(period, convertToUTC: bool = False): ...
217-
def isDuration(s): ...
218-
def stringToDate(s): ...
219-
def stringToDateTime(s, tzinfo: datetime.tzinfo | None = None, strict: bool = False) -> datetime.datetime: ...
221+
def isDuration(s: str) -> bool: ...
222+
def stringToDate(s: str) -> datetime.date: ...
223+
def stringToDateTime(s: str, tzinfo: datetime._TzInfo | None = None, strict: bool = False) -> datetime.datetime: ...
220224

221225
escapableCharList: str
222226

223-
def stringToTextValues(s, listSeparator: str = ",", charList=None, strict: bool = False): ...
224-
def stringToDurations(s, strict: bool = False): ...
227+
def stringToTextValues(
228+
s: str, listSeparator: str = ",", charList: Iterable[str] | None = None, strict: bool = False
229+
) -> list[str]: ...
230+
def stringToDurations(s: str, strict: bool = False) -> list[datetime.timedelta]: ...
225231
def parseDtstart(contentline, allowSignatureMismatch: bool = False): ...
226-
def stringToPeriod(s, tzinfo=None): ...
227-
def getTransition(transitionTo, year, tzinfo): ...
228-
def tzinfo_eq(tzinfo1, tzinfo2, startYear: int = 2000, endYear: int = 2020): ...
232+
def stringToPeriod(s: str, tzinfo=None): ...
233+
def getTransition(transitionTo: Literal["daylight", "standard"], year: int, tzinfo: datetime._TzInfo): ...
234+
def tzinfo_eq(
235+
tzinfo1: datetime._TzInfo | None, tzinfo2: datetime._TzInfo | None, startYear: int = 2000, endYear: int = 2020
236+
) -> bool: ...

stubs/vobject/vobject/ics_diff.pyi

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
def getSortKey(component): ...
2-
def sortByUID(components): ...
3-
def deleteExtraneous(component, ignore_dtstamp: bool = False) -> None: ...
1+
import optparse
2+
from collections.abc import Iterable
3+
from typing import Literal
4+
5+
from .base import Component
6+
7+
def getSortKey(component: Component) -> str: ...
8+
def sortByUID(components: Iterable[Component]) -> list[Component]: ...
9+
def deleteExtraneous(component: Component, ignore_dtstamp: bool = False) -> None: ...
410
def diff(left, right): ...
511
def prettyDiff(leftObj, rightObj) -> None: ...
612
def main() -> None: ...
7-
def getOptions(): ...
13+
def getOptions() -> tuple[Literal[False], Literal[False]] | tuple[optparse.Values, list[str]]: ...

0 commit comments

Comments
 (0)