Skip to content

Commit

Permalink
DOC: Update type hints section
Browse files Browse the repository at this point in the history
  • Loading branch information
JB Lovland authored and janbjorge committed Nov 17, 2023
1 parent 3613ee9 commit dc6c431
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
Binary file added .DS_Store
Binary file not shown.
41 changes: 18 additions & 23 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,43 +205,38 @@ Standard acronyms to start the commit message with are:
TST: addition or modification of tests
REL: related to releasing xtgeo
Type hints
Type Hints
----------

xtgeo strongly encourages (from year 2021) the use of PEP 484 style type hints.
New development should contain type hints and pull requests to annotate existing
code are accepted as well!
As of 2023, xtgeo requires the use of type annotations in all new feature
developments, incorporating Python 3.10's enhanced syntax for type hints.
This facilitates a more concise and readable style.

Style guidelines
Style Guidelines
~~~~~~~~~~~~~~~~

Types imports should follow the from typing import ... convention. So rather than
- For Python versions prior to 3.10, include the following import for compatibility:

.. code-block:: python
.. code-block:: python
import typing
primes: typing.List[int] = []
You should write
from __future__ import annotations
.. code-block:: python
from typing import List, Optional, Union
- Use Python's built-in generics (e.g., `list`, `tuple`) directly. This approach is preferred over importing types like `List` or `Tuple` from the `typing` module.

primes: List[int] = []
- Apply the new union type syntax using the pipe (`|`) for clarity and simplicity. For example:

Optional should be used where applicable, so instead of
.. code-block:: python
.. code-block:: python
primes: list[int | float] = []
maybe_primes: List[Union[int, None]] = []
- For optional types, use `None` with the pipe (`|`) instead of `Optional`. For instance:

You should write
.. code-block:: python
.. code-block:: python
maybe_primes: list[int | None] = []
maybe_primes: List[Optional[int]] = []
Note: These guidelines align with PEP 604 and are preferred for all new code submissions and when
updating existing code.


Pull Request Guidelines
Expand Down

0 comments on commit dc6c431

Please sign in to comment.