|
1 | 1 | from typing import Any, Dict, List, Optional |
2 | 2 |
|
3 | 3 | from nonebot import get_driver |
| 4 | +from pydantic import Field, BaseModel |
4 | 5 | from sentry_sdk.integrations import Integration |
| 6 | +from nonebot.compat import PYDANTIC_V2, ConfigDict |
5 | 7 | from sentry_sdk.integrations.loguru import LoguruIntegration |
6 | | -from pydantic import Extra, Field, BaseModel, validator, root_validator |
| 8 | + |
| 9 | +from .compat import field_validator, model_validator |
7 | 10 |
|
8 | 11 | driver = get_driver() |
9 | 12 |
|
10 | 13 |
|
11 | 14 | class Config(BaseModel): |
12 | 15 | sentry_dsn: Optional[str] = None |
13 | 16 | sentry_environment: str = driver.env |
14 | | - sentry_integrations: List[Integration] = Field(default_factory=list) |
| 17 | + sentry_integrations: List[Integration] = Field( |
| 18 | + default_factory=lambda: [LoguruIntegration()] |
| 19 | + ) |
15 | 20 |
|
16 | 21 | # [FIXED] https://github.com/getsentry/sentry-python/issues/653 |
17 | 22 | # sentry_default_integrations: bool = False |
18 | 23 |
|
19 | | - class Config: |
20 | | - extra = Extra.allow |
21 | | - arbitrary_types_allowed = True |
| 24 | + if PYDANTIC_V2: |
| 25 | + model_config: ConfigDict = ConfigDict( |
| 26 | + extra="allow", arbitrary_types_allowed=True |
| 27 | + ) |
| 28 | + else: |
| 29 | + |
| 30 | + class Config: |
| 31 | + extra = "allow" |
| 32 | + arbitrary_types_allowed = True |
22 | 33 |
|
23 | | - @root_validator(pre=True) |
| 34 | + @model_validator(mode="before") |
| 35 | + @classmethod |
24 | 36 | def filter_sentry_configs(cls, values: Dict[str, Any]): |
25 | 37 | return { |
26 | 38 | key: value for key, value in values.items() if key.startswith("sentry_") |
27 | 39 | } |
28 | 40 |
|
29 | | - @validator("sentry_integrations", allow_reuse=True, always=True) |
| 41 | + @field_validator("sentry_integrations", mode="after") |
30 | 42 | def validate_integrations(cls, v: List[Integration]): |
31 | 43 | ids = {i.identifier for i in v} |
32 | 44 | if LoguruIntegration.identifier not in ids: |
|
0 commit comments