Skip to content

Commit

Permalink
fix stubs and format code
Browse files Browse the repository at this point in the history
  • Loading branch information
fcurella committed Oct 7, 2024
1 parent dd1e446 commit 032a108
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
7 changes: 4 additions & 3 deletions faker/providers/date_time/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ def timestamp_to_datetime(timestamp: Union[int, float], tzinfo: Optional[TzInfo]

def maybe_format_as_string(func):
"""Format date as string if a pattern is provided."""

@functools.wraps(func)
def wrapper(*args, **kwargs):
result: dtdate = func(*args, **kwargs)
pattern = kwargs.get('pattern')
pattern = kwargs.get("pattern")
if pattern:
return result.strftime(pattern)
return result
return wrapper

return wrapper


def change_year(current_date: dtdate, year_diff: int) -> dtdate:
Expand Down Expand Up @@ -2074,7 +2075,7 @@ def date_time_between(
@maybe_format_as_string
def date_between(
self,
start_date: DateParseType = "-30y",
start_date: DateParseType = "-30y",
end_date: DateParseType = "today",
pattern: Optional[str] = None,
) -> Union[dtdate, str]:
Expand Down
59 changes: 42 additions & 17 deletions faker/proxy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -970,30 +970,34 @@ class Faker:
self,
start_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int] = ...,
end_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int] = ...,
) -> datetime.date:
pattern: Optional[str] = ...,
) -> Union[datetime.date, str]:
"""
Get a Date object based on a random date between two given dates.
Accepts date strings that can be recognized by strtotime().
:param start_date: Defaults to 30 years ago
:param end_date: Defaults to "today"
:example: Date('1999-02-02')
:return: Date
:param pattern: optional pattern to format the date as string
:example: Date('1999-02-02') or '1999-02-02'
:return: Date or string
"""
...

def date_between_dates(
self,
date_start: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int, None] = ...,
date_end: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int, None] = ...,
) -> datetime.date:
pattern: Optional[str] = ...,
) -> Union[datetime.date, str]:
"""
Takes two Date objects and returns a random date between the two given dates.
Accepts Date or datetime objects
:param date_start: Date
:param date_end: Date
:return: Date
:param pattern: optional pattern to format the date as string
:return: Date or string
"""
...

Expand All @@ -1006,8 +1010,12 @@ class Faker:
...

def date_of_birth(
self, tzinfo: Optional[datetime.tzinfo] = ..., minimum_age: int = ..., maximum_age: int = ...
) -> datetime.date:
self,
tzinfo: Optional[datetime.tzinfo] = ...,
minimum_age: int = ...,
maximum_age: int = ...,
pattern: Optional[str] = ...,
) -> Union[datetime.date, str]:
"""
Generate a random date of birth represented as a Date object,
constrained by optional miminimum_age and maximum_age
Expand All @@ -1016,51 +1024,64 @@ class Faker:
:param tzinfo: Defaults to None.
:param minimum_age: Defaults to 0.
:param maximum_age: Defaults to 115.
:param pattern: optional pattern to format the date as string
:example: Date('1979-02-02')
:return: Date
"""
...

def date_this_century(self, before_today: bool = ..., after_today: bool = ...) -> datetime.date:
def date_this_century(
self, before_today: bool = ..., after_today: bool = ..., pattern: Optional[str] = ...
) -> Union[datetime.date, str]:
"""
Gets a Date object for the current century.
:param before_today: include days in current century before today
:param after_today: include days in current century after today
:param pattern: optional pattern to format the date as string
:example: Date('2012-04-04')
:return: Date
:return: Date or string
"""
...

def date_this_decade(self, before_today: bool = ..., after_today: bool = ...) -> datetime.date:
def date_this_decade(
self, before_today: bool = ..., after_today: bool = ..., pattern: Optional[str] = ...
) -> Union[datetime.date, str]:
"""
Gets a Date object for the decade year.
:param before_today: include days in current decade before today
:param after_today: include days in current decade after today
:param pattern: optional pattern to format the date as string
:example: Date('2012-04-04')
:return: Date
:return: Date or string
"""
...

def date_this_month(self, before_today: bool = ..., after_today: bool = ...) -> datetime.date:
def date_this_month(
self, before_today: bool = ..., after_today: bool = ..., pattern: Optional[str] = ...
) -> Union[datetime.date, str]:
"""
Gets a Date object for the current month.
:param before_today: include days in current month before today
:param after_today: include days in current month after today
:param pattern: optional pattern to format the date as string
:example: dtdate('2012-04-04')
:return: dtdate
:return: dtdate or string
"""
...

def date_this_year(self, before_today: bool = ..., after_today: bool = ...) -> datetime.date:
def date_this_year(
self, before_today: bool = ..., after_today: bool = ..., pattern: Optional[str] = ...
) -> Union[datetime.date, str]:
"""
Gets a Date object for the current year.
:param before_today: include days in current year before today
:param after_today: include days in current year after today
:param pattern: optional pattern to format the date as string
:example: Date('2012-04-04')
:return: Date
"""
Expand Down Expand Up @@ -1194,14 +1215,16 @@ class Faker:
self,
end_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int] = ...,
tzinfo: Optional[datetime.tzinfo] = ...,
) -> datetime.date:
pattern: Optional[str] = ...,
) -> Union[datetime.date, str]:
"""
Get a Date object based on a random date between 1 day from now and a
given date.
Accepts date strings that can be recognized by strtotime().
:param end_date: Defaults to "+30d"
:param tzinfo: timezone, instance of datetime.tzinfo subclass
:param pattern: optional pattern to format the date as string
:example: dtdate('2030-01-01')
:return: dtdate
"""
Expand Down Expand Up @@ -1247,16 +1270,18 @@ class Faker:
self,
start_date: Union[datetime.date, datetime.datetime, datetime.timedelta, str, int] = ...,
tzinfo: Optional[datetime.tzinfo] = ...,
) -> datetime.date:
pattern: Optional[str] = ...,
) -> Union[datetime.date, str]:
"""
Get a Date object based on a random date between a given date and 1 day
ago.
Accepts date strings that can be recognized by strtotime().
:param start_date: Defaults to "-30d"
:param tzinfo: timezone, instance of datetime.tzinfo subclass
:param pattern: optional pattern to format the date as string
:example: dtdate('1999-02-02')
:return: dtdate
:return: dtdate or string
"""
...

Expand Down

0 comments on commit 032a108

Please sign in to comment.