![github-actions[bot]](/assets/img/avatar_default.png)
* Create rule S6882 * Specify rule * CR fixes * CR fixes * Remove extra comments --------- Co-authored-by: maksim-grebeniuk-sonarsource <maksim-grebeniuk-sonarsource@users.noreply.github.com> Co-authored-by: Maksim Grebeniuk <maksim.grebeniuk@sonarsource.com> Co-authored-by: Ghislain Piot <ghislain.piot@sonarsource.com>
36 lines
1004 B
Plaintext
36 lines
1004 B
Plaintext
This rule raises an issue when an incorrect value is set as an attribute of `datetime.date`, `datetime.time`, or `datetime.datetime`
|
|
|
|
== Why is this an issue?
|
|
|
|
Setting a date attribute value with a value which is out of the range of possible values will lead to a `ValueError`.
|
|
|
|
== How to fix it
|
|
Set attribute values with values that are within the range of possible values.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,python,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
def foo():
|
|
dt = datetime(year=2024, day=66, month=1, hour=16, minute=1) # ValueError: day is out of range for month
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,python,diff-id=1,diff-type=compliant]
|
|
----
|
|
def foo():
|
|
dt = datetime(year=2024, day=1, month=1, hour=16, minute=1)
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
* Python documentation - https://docs.python.org/3/library/datetime.html#[datetime]
|
|
//=== Articles & blog posts
|
|
//=== Conference presentations
|
|
//=== Standards
|
|
//=== External coding guidelines
|
|
//=== Benchmarks
|