Skip to content

Commit

Permalink
Adding support for pydantic-v1 (#128)
Browse files Browse the repository at this point in the history
* Adding support for pydantic-v1
* Adding extras to Github Actions
* updates to poetry lock and pyproject.toml
  • Loading branch information
vminfant authored Mar 4, 2024
1 parent 047abdd commit 0d5307e
Show file tree
Hide file tree
Showing 8 changed files with 547 additions and 509 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip poetry==1.2.2
poetry install
poetry install --extras "settings"
- name: Linting
run: |
Expand All @@ -33,7 +33,7 @@ jobs:
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip poetry==1.2.2
poetry install
poetry install --extras "settings"
- name: Run tests
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
run: |
python3 -m pip install --upgrade pip poetry==1.2.2
poetry config virtualenvs.create false
poetry install
poetry install --extras "settings"
- name: Linting
run: |
Expand Down
3 changes: 2 additions & 1 deletion cognite/cdffs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Initialize the cdffs package."""

import fsspec

from .spec import CdfFileSystem

__version__ = "0.3.3"
__version__ = "0.3.4"
__all__ = ["CdfFileSystem"]

fsspec.register_implementation(CdfFileSystem.protocol, CdfFileSystem)
32 changes: 27 additions & 5 deletions cognite/cdffs/credentials.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
"""Construct cognite client config from environment variables."""

from abc import ABC, abstractmethod
from typing import Any, List, Optional, Union

from cognite.client import ClientConfig, CogniteClient
from cognite.client.credentials import OAuthClientCredentials, Token
from pydantic import ConfigDict, SecretStr, field_validator
from pydantic_settings import BaseSettings

try:
from pydantic import BaseSettings, ConfigDict, SecretStr, validator

is_pydantic_v2 = False
except ImportError:
from pydantic import ConfigDict, SecretStr, field_validator
from pydantic_settings import BaseSettings

is_pydantic_v2 = True


def validate_scopes(cls: Any, value: str) -> Optional[List]:
Expand Down Expand Up @@ -70,7 +79,10 @@ class FsOAuthCredentials(FsCredentials, FsConfig):
scopes: Optional[Union[str, List]] = None

# Validator
_scopes = field_validator("scopes")(validate_scopes)
if is_pydantic_v2:
_scopes = field_validator("scopes")(validate_scopes)
else:
_scopes = validator("scopes")(validate_scopes)

def get_credentials(self) -> OAuthClientCredentials:
"""Construct credentials based on environment variables.
Expand Down Expand Up @@ -112,11 +124,21 @@ def get_connection_config(env_file: str) -> CogniteClient:
"""Construct Cognite Client from environment variables."""
credentials = FsOAuthCredentials(_env_file=env_file)
connection_config = None
if all(value is not None for _, value in credentials.model_dump().items()):

if is_pydantic_v2:
credentials_dump = credentials.model_dump().items()
else:
credentials_dump = credentials.dict().items()

if all(value is not None for _, value in credentials_dump):
connection_config = credentials.get_client_config()
else:
token = FsToken(_env_file=env_file)
if all(value is not None for _, value in token.model_dump().items()):
if is_pydantic_v2:
token_dump = token.model_dump().items()
else:
token_dump = token.dict().items()
if all(value is not None for _, value in token_dump):
connection_config = token.get_client_config()

return connection_config
8 changes: 4 additions & 4 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
toml==0.10.2
fsspec==2023.10.0
cognite-sdk==7.1.0
fsspec==2024.2.0
cognite-sdk==7.26.0
requests==2.31.0
pydantic==2.5.1
sphinx-rtd-theme==1.3.0
pydantic==2.6.3
sphinx-rtd-theme==2.0.0
tenacity==8.2.3
pydantic_settings==2.1.0
8 changes: 7 additions & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ the list of all supported/compatible python packages.

Installation
^^^^^^^^^^^^
To install this package:
To install this package(Recommended):

.. code-block:: bash
pip install cognite-cdffs[settings]
If you need `cdffs` to be compatible with pydantic-v1, choose to install the expected pydantic-v1 (`^1.10.7`) and use,

.. code-block:: bash
Expand Down
969 changes: 489 additions & 480 deletions poetry.lock

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cognite-cdffs"
version = "0.3.3"
version = "0.3.4"
description = "File System Interface for CDF Files"
license = "Apache-2.0"
authors = ["Infant Alex <[email protected]>"]
Expand All @@ -9,23 +9,22 @@ packages = [
{ include="cognite", from="." },
]
[tool.poetry.group.test.dependencies]
pandas = "^2.2.0"
pandas = "^2.2.1"
pyarrow = "^15.0.0"
zarr = "^2.16.1"
dask = "^2024.1.1"
xarray = "^2024.1.1"
zarr = "^2.17.0"
dask = "^2024.2.1"
xarray = "^2024.2.0"
geodatasets = "^2023.12.0"
geopandas = "^0.14.3"

[tool.poetry.group.dev.dependencies]
types-requests = "^2.28.11.5"
types-requests = "^2.31.0.20240218"
pytest-cov = "^4.1.0"
black = "^24.1.1"
responses = "^0.24.1"
black = "^24.2.0"
responses = "^0.25.0"
flake8 = "^7.0.0"
pre-commit = "^3.6.0"
pre-commit = "^3.6.2"
flake8-pyproject = "^1.2.3"
twine = "^4.0.2"
toml = "^0.10.2"
sphinx-rtd-theme = "^2.0.0"

Expand Down Expand Up @@ -68,16 +67,17 @@ commands =

[tool.poetry.dependencies]
python = ">=3.9.10,<3.13"
cognite-sdk = "^7.16.0"
fsspec = "^2023.12.2"
cognite-sdk = "^7.26.0"
fsspec = "^2024.2.0"
requests = "^2.31.0"
twine = "^4.0.2"
pydantic = "^2.6.0"
pydantic = {version =">=1.10.7"}
python-dotenv = "^1.0.1"
pydantic-settings = "^2.0.3"
pydantic-settings = { version = "^2.0.3", optional = true}
tenacity = "^8.2.3"

[tool.poetry.dev-dependencies]
[tool.poetry.extras]
settings = ["pydantic-settings"]

[build-system]
requires = ["poetry>=0.12"]
Expand Down

0 comments on commit 0d5307e

Please sign in to comment.