* pydantic v2 compatability

Drops usage of pydantic-settings. *Technically* this is a breaking
change, since none of the pydantic methods will be available on the new
Settings object.
This commit is contained in:
Tom Augspurger 2023-07-05 07:20:43 -05:00 коммит произвёл GitHub
Родитель 14f4df8593
Коммит 1a61bceefe
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 78 добавлений и 37 удалений

Просмотреть файл

@ -1,3 +1,16 @@
# 1.0.0
## Bug fixes
* Compatibility with `pydantic>=2.0` (https://github.com/microsoft/planetary-computer-sdk-for-python/pull/59)
## API Breaking Changes
* `planetary_computer.settings.Settings()` is no longer a pydantic Model. To support both pydantic 1.x and 2.x,
the implementation of `Settings` changed. There aren't any user-facing changes in the primary API exposed by
`Settings`, around creating the settings object and getting / setting values. But it no longer subclasses
`pydantic.BaseModel` (https://github.com/microsoft/planetary-computer-sdk-for-python/pull/59).
# 0.5.1
## Bug fixes

Просмотреть файл

@ -9,6 +9,8 @@ from functools import singledispatch
from urllib.parse import urlparse, parse_qs
import requests
import requests.adapters
import packaging.version
import pydantic
from pydantic import BaseModel, Field
from pystac import Asset, Item, ItemCollection, STACObjectType, Collection
from pystac.utils import datetime_to_str
@ -26,6 +28,10 @@ from planetary_computer.utils import (
asset_xpr,
)
_PYDANTIC_2_0 = packaging.version.parse(
pydantic.__version__
) >= packaging.version.parse("2.0.0")
BLOB_STORAGE_DOMAIN = ".blob.core.windows.net"
AssetLike = TypeVar("AssetLike", Asset, Dict[str, Any])
@ -38,8 +44,11 @@ class SASBase(BaseModel):
"""RFC339 datetime format of the time this token will expire"""
class Config:
json_encoders = {datetime: datetime_to_str}
allow_population_by_field_name = True
if _PYDANTIC_2_0:
populate_by_name = True
else:
allow_population_by_field_name = True
json_encoders = {datetime: datetime_to_str}
class SignedLink(SASBase):

Просмотреть файл

@ -1,6 +1,9 @@
import os
from functools import lru_cache
from typing import Optional
import pydantic
import dataclasses
import dotenv
SETTINGS_ENV_FILE = "~/.planetarycomputer/settings.env"
SETTINGS_ENV_PREFIX = "PC_SDK_"
@ -8,37 +11,6 @@ SETTINGS_ENV_PREFIX = "PC_SDK_"
DEFAULT_SAS_TOKEN_ENDPOINT = "https://planetarycomputer.microsoft.com/api/sas/v1/token"
class Settings(pydantic.BaseSettings):
"""PC SDK configuration settings
Settings defined here are attempted to be read in two ways, in this order:
* environment variables
* environment file: ~/.planetarycomputer/settings.env
That is, any settings defined via environment variables will take precedence
over settings defined in the environment file, so can be used to override.
All settings are prefixed with `PC_SDK_`
"""
# PC_SDK_SUBSCRIPTION_KEY: subscription key to send along with token
# requests. If present, allows less restricted rate limiting.
subscription_key: Optional[str] = None
# PC_SDK_SAS_URL: The planetary computer SAS endpoint URL.
# This will default to the main planetary computer endpoint.
sas_url: str = DEFAULT_SAS_TOKEN_ENDPOINT
class Config:
env_file = SETTINGS_ENV_FILE
env_prefix = SETTINGS_ENV_PREFIX
@staticmethod
@lru_cache(maxsize=1)
def get() -> "Settings":
return Settings()
def set_subscription_key(key: str) -> None:
"""Sets the Planetary Computer API subscription key to use
within the process that loaded this module. Ths does not write
@ -50,3 +22,44 @@ def set_subscription_key(key: str) -> None:
such as SAS token generation.
"""
Settings.get().subscription_key = key
def _from_env(key: str) -> Optional[str]:
value = os.environ.get(key)
if value is None:
dotenv.load_dotenv(os.path.expanduser(SETTINGS_ENV_FILE))
value = os.environ.get(key)
return value
def _subscription_key_default() -> Optional[str]:
return _from_env("PC_SDK_SUBSCRIPTION_KEY")
def _sas_url_default() -> str:
return _from_env("PC_SDK_SAS_URL") or DEFAULT_SAS_TOKEN_ENDPOINT
@dataclasses.dataclass
class Settings:
"""PC SDK configuration settings
Settings defined here are attempted to be read in two ways, in this order:
* environment variables
* environment file: ~/.planetarycomputer/settings.env
That is, any settings defined via environment variables will take precedence
over settings defined in the environment file, so can be used to override.
All settings are prefixed with `PC_SDK_`
"""
subscription_key: Optional[str] = dataclasses.field(
default_factory=_subscription_key_default
)
sas_url: Optional[str] = dataclasses.field(default_factory=_sas_url_default)
@staticmethod
@lru_cache(maxsize=1)
def get() -> "Settings":
return Settings()

Просмотреть файл

@ -1,3 +1,3 @@
"""Library version"""
__version__ = "0.5.1"
__version__ = "1.0.0"

Просмотреть файл

@ -4,18 +4,21 @@ build-backend = "setuptools.build_meta"
[project]
name = "planetary-computer"
version = "0.5.1"
authors = [{name = "microsoft", email = "planetarycomputer@microsoft.com"}]
description = "Planetary Computer SDK for Python"
requires-python = ">=3.7"
dependencies = [
"click>=7.1",
"pydantic[dotenv]>=1.7.3",
"pydantic>=1.7.3",
"pystac>=1.0.0",
"pystac-client>=0.2.0",
"pytz>=2020.5",
"requests>=2.25.1",
"packaging",
"python-dotenv",
]
dynamic = ["version"]
readme = "README.md"
license = {file = "LICENSE"}
@ -37,3 +40,6 @@ planetarycomputer = "planetary_computer.scripts.cli:app"
[tool.setuptools]
include-package-data = false
[tool.setuptools.dynamic]
version = {attr = "planetary_computer.version.__version__"}