Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/modelscope_hub/_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def get_current_username(self) -> str:
data = self.get_current_user()
if not isinstance(data, dict):
return ""
for key in ("Username", "username", "preferred_username", "name"):
for key in ("Username", "username", "name", "preferred_username"):
value = data.get(key)
if value:
return str(value)
Expand Down
12 changes: 10 additions & 2 deletions src/modelscope_hub/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from dataclasses import dataclass, field, fields
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Generic, TypedDict, TypeVar
from typing import Any, ClassVar, Generic, TypedDict, TypeVar

from .constants import RepoType, Visibility

Expand Down Expand Up @@ -40,12 +40,15 @@ def _coerce_datetime(value: Any) -> datetime | None:
class _FromDictMixin:
"""Adds tolerant ``from_dict`` construction to a dataclass."""

_field_aliases: ClassVar[dict[str, str]] = {}

@classmethod
def from_dict(cls: type[_TDataclass], data: Mapping[str, Any] | None) -> _TDataclass:
if not data:
return cls() # type: ignore[call-arg]
known = {f.name for f in fields(cls)} # type: ignore[arg-type]
kwargs = {key: value for key, value in data.items() if key in known}
aliases = cls._field_aliases
kwargs = {aliases.get(key, key): value for key, value in data.items() if aliases.get(key, key) in known}
return cls(**kwargs) # type: ignore[arg-type]


Expand All @@ -54,6 +57,11 @@ def from_dict(cls: type[_TDataclass], data: Mapping[str, Any] | None) -> _TDatac
# ---------------------------------------------------------------------------
@dataclass(slots=True)
class UserInfo(_FromDictMixin):
_field_aliases: ClassVar[dict[str, str]] = {
"name": "username",
"avatar": "avatar_url",
}

id: str | int | None = None
username: str | None = None
email: str | None = None
Expand Down
Loading