Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List organizations in get_user_overview #2404

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions src/huggingface_hub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@
"CommitOperationAdd",
"CommitOperationCopy",
"CommitOperationDelete",
"DatasetInfo",
"GitCommitInfo",
"GitRefInfo",
"GitRefs",
"HfApi",
"ModelInfo",
"RepoUrl",
"SpaceInfo",
"User",
"UserLikes",
"WebhookInfo",
Expand Down Expand Up @@ -199,6 +202,7 @@
"get_space_runtime",
"get_space_variables",
"get_token_permission",
"get_user_overview",
"get_webhook",
"grant_access",
"like",
Expand Down Expand Up @@ -643,11 +647,14 @@ def __dir__():
CommitOperationAdd, # noqa: F401
CommitOperationCopy, # noqa: F401
CommitOperationDelete, # noqa: F401
DatasetInfo, # noqa: F401
GitCommitInfo, # noqa: F401
GitRefInfo, # noqa: F401
GitRefs, # noqa: F401
HfApi, # noqa: F401
ModelInfo, # noqa: F401
RepoUrl, # noqa: F401
SpaceInfo, # noqa: F401
User, # noqa: F401
UserLikes, # noqa: F401
WebhookInfo, # noqa: F401
Expand Down Expand Up @@ -699,6 +706,7 @@ def __dir__():
get_space_runtime, # noqa: F401
get_space_variables, # noqa: F401
get_token_permission, # noqa: F401
get_user_overview, # noqa: F401
get_webhook, # noqa: F401
grant_access, # noqa: F401
like, # noqa: F401
Expand Down
57 changes: 43 additions & 14 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,33 @@ class UserLikes:
spaces: List[str]


@dataclass
class Organization:
"""
Contains information about an organization on the Hub.

Attributes:
avatar_url (`str`):
URL of the organization's avatar.
name (`str`):
Name of the organization on the Hub (unique).
fullname (`str`):
Organization's full name.
"""

avatar_url: str
name: str
fullname: str

def __init__(self, **kwargs) -> None:
self.avatar_url = kwargs.pop("avatarUrl", "")
self.name = kwargs.pop("name", "")
self.fullname = kwargs.pop("fullname", "")

# forward compatibility
self.__dict__.update(**kwargs)


@dataclass
class User:
"""
Expand Down Expand Up @@ -1347,22 +1374,24 @@ class User:
num_likes: Optional[int] = None
is_following: Optional[bool] = None
details: Optional[str] = None
orgs: List[Organization] = field(default_factory=list)

def __init__(self, **kwargs) -> None:
self.avatar_url = kwargs.get("avatarUrl", "")
self.username = kwargs.get("user", "")
self.fullname = kwargs.get("fullname", "")
self.is_pro = kwargs.get("isPro")
self.num_models = kwargs.get("numModels")
self.num_datasets = kwargs.get("numDatasets")
self.num_spaces = kwargs.get("numSpaces")
self.num_discussions = kwargs.get("numDiscussions")
self.num_papers = kwargs.get("numPapers")
self.num_upvotes = kwargs.get("numUpvotes")
self.num_likes = kwargs.get("numLikes")
self.user_type = kwargs.get("type")
self.is_following = kwargs.get("isFollowing")
self.details = kwargs.get("details")
self.avatar_url = kwargs.pop("avatarUrl", "")
self.username = kwargs.pop("user", "")
self.fullname = kwargs.pop("fullname", "")
self.is_pro = kwargs.pop("isPro", None)
self.num_models = kwargs.pop("numModels", None)
self.num_datasets = kwargs.pop("numDatasets", None)
self.num_spaces = kwargs.pop("numSpaces", None)
self.num_discussions = kwargs.pop("numDiscussions", None)
self.num_papers = kwargs.pop("numPapers", None)
self.num_upvotes = kwargs.pop("numUpvotes", None)
self.num_likes = kwargs.pop("numLikes", None)
self.user_type = kwargs.pop("type", None)
self.is_following = kwargs.pop("isFollowing", None)
self.details = kwargs.pop("details", None)
self.orgs = [Organization(**org) for org in kwargs.pop("orgs", [])]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add to User class docstring

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in fe04844


# forward compatibility
self.__dict__.update(**kwargs)
Expand Down
15 changes: 9 additions & 6 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3998,21 +3998,24 @@ def setUpClass(cls) -> None:

def test_user_overview(self) -> None:
overview = self.api.get_user_overview("julien-c")
self.assertEqual(overview.user_type, "user")
self.assertGreater(overview.num_likes, 10)
self.assertGreater(overview.num_upvotes, 10)
assert overview.user_type == "user"
assert overview.username == "julien-c"
assert overview.num_likes > 10
assert overview.num_upvotes > 10
assert len(overview.orgs) > 0
assert any(org.name == "huggingface" for org in overview.orgs)

def test_organization_members(self) -> None:
members = self.api.list_organization_members("huggingface")
self.assertGreater(len(list(members)), 1)
assert len(list(members)) > 1

def test_user_followers(self) -> None:
followers = self.api.list_user_followers("julien-c")
self.assertGreater(len(list(followers)), 10)
assert len(list(followers)) > 10

def test_user_following(self) -> None:
following = self.api.list_user_following("julien-c")
self.assertGreater(len(list(following)), 10)
assert len(list(following)) > 10


class WebhookApiTest(HfApiCommonTest):
Expand Down
Loading