Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Versions follow `Semantic Versioning <http://www.semver.org>`_
---------------------
Changed
~~~~~~~
* Raise `TypeError` instead of `ValueError` if constructor is called with value of wrong type.
* Raise ``TypeError`` instead of ``ValueError`` if constructor is called with value of wrong type.
* Update ``ruff`` linter rules and switch to ``hatch fmt``.

Added
Expand Down Expand Up @@ -71,7 +71,7 @@ Added
~~~~~
* Added a new flag ``--uuid4`` to the CLI ``show`` command, that converts the provided ``ULID``
into an RFC 4122 compliant ``UUID``.
* The `ulid build` command allows the use of the special value ``-`` for all options to read its
* The ``ulid build`` command allows the use of the special value ``-`` for all options to read its
inputs from ``stdin``. E.g.

.. code-block:: bash
Expand Down Expand Up @@ -128,7 +128,7 @@ Changed
Added
~~~~~
* Added support for Python 3.10.
* Added :attr:`__version__` variable to package.
* Added ``__version__`` variable to package.


`1.0.3`_ - 2021-07-14
Expand All @@ -153,7 +153,7 @@ Added
Changed
~~~~~~~
* Dropped support for Python 2. Only Python 3.6+ is supported.
* The named constructor :meth:`.ULID.new` has been removed. Use one of the specifc named
* The named constructor ``ULID.new`` has been removed. Use one of the specifc named
constructors instead. For a new :class:`.ULID` created from the current timestamp use the
standard constructor.

Expand All @@ -169,8 +169,8 @@ Changed
ulid = ULID.from_timestamp(time.time())
ulid = ULID.from_datetime(datetime.now())

* The :meth:`.ULID.str` and :meth:`.ULID.int` methods have been removed in favour of the more
Pythonic special dunder-methods. Use `str(ulid)` and `int(ulid)` instead.
* The ``ULID.str`` and ``ULID.int`` methods have been removed in favour of the more
Pythonic special dunder-methods. Use ``str(ulid)`` and ``int(ulid)`` instead.
* Added the property :meth:`.ULID.hex` that returns a hex representation of the :class:`.ULID`.

.. code-block:: python
Expand Down
13 changes: 6 additions & 7 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
copyright = f"{datetime.now(timezone.utc).year}, Martin Domke"
author = "Martin Domke"
master_doc = "index"
source_suffix = [".rst", ".md"]
source_suffix = {".rst": "restructuredtext", ".md": "restructuredtext"}

# The full version, including alpha/beta/rc tags
release = ulid.__version__
Expand All @@ -23,6 +23,7 @@
# ones.
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.intersphinx",
"sphinx.ext.napoleon",
"sphinx.ext.viewcode",
"sphinx_copybutton",
Expand All @@ -31,11 +32,6 @@
# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []


# -- Options for HTML output -------------------------------------------------

Expand Down Expand Up @@ -76,7 +72,10 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
pygments_style = "sphinx"

autodoc_member_order = "groupwise"
# ----------------------------------------------

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {"python": ("https://docs.python.org/3", None)}
19 changes: 10 additions & 9 deletions ulid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

import datetime
import functools
import os
import time
import uuid
from datetime import datetime
from datetime import timezone
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is much nicer to write and read code that does from datetime import datetime, timezone

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really necessary in order to get rid of the Sphinx warnings?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be other ways!

from typing import Any
from typing import cast
from typing import Generic
Expand Down Expand Up @@ -87,8 +86,8 @@ def __init__(self, value: bytes | None = None) -> None:
)

@classmethod
@validate_type(datetime)
def from_datetime(cls, value: datetime) -> Self:
@validate_type(datetime.datetime)
def from_datetime(cls, value: datetime.datetime) -> Self:
"""Create a new :class:`ULID`-object from a :class:`datetime`. The timestamp part of the
`ULID` will be set to the corresponding timestamp of the datetime.

Expand Down Expand Up @@ -161,8 +160,9 @@ def from_int(cls, value: int) -> Self:
def parse(cls, value: Any) -> Self:
"""Create a new :class:`ULID`-object from a given value.

.. note:: This method should only be used when the caller is trying to parse a ULID from
a value when they're unsure what format/primitive type it will be given in.
.. note::
This method should only be used when the caller is trying to parse a ULID from
a value when they're unsure what format/primitive type it will be given in.
"""
if isinstance(value, ULID):
return cast(Self, value)
Expand All @@ -183,7 +183,7 @@ def parse(cls, value: Any) -> Self:
return cls.from_timestamp(value)
if isinstance(value, float):
return cls.from_timestamp(value)
if isinstance(value, datetime):
if isinstance(value, datetime.datetime):
return cls.from_datetime(value)
if isinstance(value, bytes):
return cls.from_bytes(value)
Expand Down Expand Up @@ -211,16 +211,17 @@ def timestamp(self) -> float:
"""
return self.milliseconds / constants.MILLISECS_IN_SECS


@functools.cached_property
def datetime(self) -> datetime:
def datetime(self) -> datetime.datetime:
"""Return the timestamp part as timezone-aware :class:`datetime` in UTC.

Examples:

>>> ulid.datetime
datetime.datetime(2020, 4, 30, 14, 33, 27, 560000, tzinfo=datetime.timezone.utc)
"""
return datetime.fromtimestamp(self.timestamp, timezone.utc)
return datetime.datetime.fromtimestamp(self.timestamp, datetime.timezone.utc)

@functools.cached_property
def hex(self) -> str:
Expand Down
Loading