Skip to content

Commit

Permalink
Merge pull request #289 from specklesystems/gergo/pydantic2
Browse files Browse the repository at this point in the history
gergo/pydantic2
  • Loading branch information
gjedlicska authored Aug 14, 2023
2 parents 8a91260 + 8a34b95 commit cca7b18
Show file tree
Hide file tree
Showing 8 changed files with 730 additions and 681 deletions.
1,252 changes: 651 additions & 601 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ packages = [

[tool.poetry.dependencies]
python = ">=3.7.2, <4.0"
pydantic = "^1.9"
pydantic = "^2.0"
appdirs = "^1.4.4"
gql = {extras = ["requests", "websockets"], version = "^3.3.0"}
ujson = "^5.3.0"
Expand Down
6 changes: 4 additions & 2 deletions src/specklepy/core/api/credentials.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path
from typing import List, Optional

from pydantic import BaseModel, Field # pylint: disable=no-name-in-module
Expand Down Expand Up @@ -57,7 +58,7 @@ def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
res = account_storage.get_all_objects()
account_storage.close()
if res:
accounts.extend(Account.parse_raw(r[1]) for r in res)
accounts.extend(Account.model_validate_json(r[1]) for r in res)
except SpeckleException:
# cannot open SQLiteTransport, probably because of the lack
# of disk write permissions
Expand All @@ -78,7 +79,8 @@ def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
if json_acct_files:
try:
accounts.extend(
Account.parse_file(os.path.join(json_path, json_file))
Account.model_validate_json(Path(json_path, json_file).read_text())
# Account.parse_file(os.path.join(json_path, json_file))
for json_file in json_acct_files
)
except Exception as ex:
Expand Down
132 changes: 66 additions & 66 deletions src/specklepy/core/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@


class Collaborator(BaseModel):
id: Optional[str]
name: Optional[str]
role: Optional[str]
avatar: Optional[str]
id: Optional[str] = None
name: Optional[str] = None
role: Optional[str] = None
avatar: Optional[str] = None


class Commit(BaseModel):
id: Optional[str]
message: Optional[str]
authorName: Optional[str]
authorId: Optional[str]
authorAvatar: Optional[str]
branchName: Optional[str]
createdAt: Optional[datetime]
sourceApplication: Optional[str]
referencedObject: Optional[str]
totalChildrenCount: Optional[int]
parents: Optional[List[str]]
id: Optional[str] = None
message: Optional[str] = None
authorName: Optional[str] = None
authorId: Optional[str] = None
authorAvatar: Optional[str] = None
branchName: Optional[str] = None
createdAt: Optional[datetime] = None
sourceApplication: Optional[str] = None
referencedObject: Optional[str] = None
totalChildrenCount: Optional[int] = None
parents: Optional[List[str]] = None

def __repr__(self) -> str:
return (
Expand All @@ -36,35 +36,35 @@ def __str__(self) -> str:


class Commits(BaseModel):
totalCount: Optional[int]
cursor: Optional[datetime]
totalCount: Optional[int] = None
cursor: Optional[datetime] = None
items: List[Commit] = []


class Object(BaseModel):
id: Optional[str]
speckleType: Optional[str]
applicationId: Optional[str]
totalChildrenCount: Optional[int]
createdAt: Optional[datetime]
id: Optional[str] = None
speckleType: Optional[str] = None
applicationId: Optional[str] = None
totalChildrenCount: Optional[int] = None
createdAt: Optional[datetime] = None


class Branch(BaseModel):
id: Optional[str]
name: Optional[str]
description: Optional[str]
commits: Optional[Commits]
id: Optional[str] = None
name: Optional[str] = None
description: Optional[str] = None
commits: Optional[Commits] = None


class Branches(BaseModel):
totalCount: Optional[int]
cursor: Optional[datetime]
totalCount: Optional[int] = None
cursor: Optional[datetime] = None
items: List[Branch] = []


class Stream(BaseModel):
id: Optional[str] = None
name: Optional[str]
name: Optional[str] = None
role: Optional[str] = None
isPublic: Optional[bool] = None
description: Optional[str] = None
Expand All @@ -89,21 +89,21 @@ def __str__(self) -> str:


class Streams(BaseModel):
totalCount: Optional[int]
cursor: Optional[datetime]
totalCount: Optional[int] = None
cursor: Optional[datetime] = None
items: List[Stream] = []


class User(BaseModel):
id: Optional[str]
email: Optional[str]
name: Optional[str]
bio: Optional[str]
company: Optional[str]
avatar: Optional[str]
verified: Optional[bool]
role: Optional[str]
streams: Optional[Streams]
id: Optional[str] = None
email: Optional[str] = None
name: Optional[str] = None
bio: Optional[str] = None
company: Optional[str] = None
avatar: Optional[str] = None
verified: Optional[bool] = None
role: Optional[str] = None
streams: Optional[Streams] = None

def __repr__(self):
return (
Expand All @@ -119,24 +119,24 @@ class LimitedUser(BaseModel):
"""Limited user type, for showing public info about a user to another user."""

id: str
name: Optional[str]
bio: Optional[str]
company: Optional[str]
avatar: Optional[str]
verified: Optional[bool]
role: Optional[str]
name: Optional[str] = None
bio: Optional[str] = None
company: Optional[str] = None
avatar: Optional[str] = None
verified: Optional[bool] = None
role: Optional[str] = None


class PendingStreamCollaborator(BaseModel):
id: Optional[str]
inviteId: Optional[str]
streamId: Optional[str]
streamName: Optional[str]
title: Optional[str]
role: Optional[str]
invitedBy: Optional[User]
user: Optional[User]
token: Optional[str]
id: Optional[str] = None
inviteId: Optional[str] = None
streamId: Optional[str] = None
streamName: Optional[str] = None
title: Optional[str] = None
role: Optional[str] = None
invitedBy: Optional[User] = None
user: Optional[User] = None
token: Optional[str] = None

def __repr__(self):
return (
Expand All @@ -150,14 +150,14 @@ def __str__(self) -> str:


class Activity(BaseModel):
actionType: Optional[str]
info: Optional[dict]
userId: Optional[str]
streamId: Optional[str]
resourceId: Optional[str]
resourceType: Optional[str]
message: Optional[str]
time: Optional[datetime]
actionType: Optional[str] = None
info: Optional[dict] = None
userId: Optional[str] = None
streamId: Optional[str] = None
resourceId: Optional[str] = None
resourceType: Optional[str] = None
message: Optional[str] = None
time: Optional[datetime] = None

def __repr__(self) -> str:
return (
Expand All @@ -170,9 +170,9 @@ def __str__(self) -> str:


class ActivityCollection(BaseModel):
totalCount: Optional[int]
items: Optional[List[Activity]]
cursor: Optional[datetime]
totalCount: Optional[int] = None
items: Optional[List[Activity]] = None
cursor: Optional[datetime] = None

def __repr__(self) -> str:
return (
Expand Down
4 changes: 2 additions & 2 deletions src/specklepy/core/api/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def _parse_response(self, response: Union[dict, list, None], schema=None):
if isinstance(response, list):
return [self._parse_response(response=r, schema=schema) for r in response]
if schema:
return schema.parse_obj(response)
return schema.model_validate(response)
elif self.schema:
try:
return self.schema.parse_obj(response)
return self.schema.model_validate(response)
except Exception:
s = BaseObjectSerializer(read_transport=SQLiteTransport())
return s.recompose_base(response)
Expand Down
5 changes: 1 addition & 4 deletions src/specklepy/transports/abstract_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class AbstractTransport(ABC, BaseModel):
_name: str = "Abstract"
model_config = {'extra': 'allow', 'arbitrary_types_allowed': True}

@property
def name(self):
Expand Down Expand Up @@ -87,7 +88,3 @@ def copy_object_and_children(
str -- the string representation of the root object
"""
pass

class Config:
extra = Extra.allow
arbitrary_types_allowed = True
2 changes: 1 addition & 1 deletion tests/intergration/test_active_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_user_activity(self, client: SpeckleClient, second_user_dict):
assert my_activity.totalCount
assert isinstance(their_activity, ActivityCollection)

older_activity = client.user.activity(before=my_activity.items[0].time)
older_activity = client.active_user.activity(before=my_activity.items[0].time)

assert isinstance(older_activity, ActivityCollection)
assert older_activity.totalCount
Expand Down
8 changes: 4 additions & 4 deletions tests/intergration/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def updated_stream(

@pytest.fixture(scope="module")
def second_user(self, second_client: SpeckleClient):
return second_client.user.get()
return second_client.active_user.get()

def test_stream_create(self, client, stream, updated_stream):
stream.id = updated_stream.id = client.stream.create(
Expand Down Expand Up @@ -113,18 +113,18 @@ def test_stream_invite_get_all_for_user(
self, second_client: SpeckleClient, stream: Stream
):
# NOTE: these are user queries, but testing here to contain the flow
invites = second_client.user.get_all_pending_invites()
invites = second_client.active_user.get_all_pending_invites()

assert isinstance(invites, list)
assert isinstance(invites[0], PendingStreamCollaborator)
assert len(invites) == 1

invite = second_client.user.get_pending_invite(stream_id=stream.id)
invite = second_client.active_user.get_pending_invite(stream_id=stream.id)
assert isinstance(invite, PendingStreamCollaborator)

def test_stream_invite_use(self, second_client: SpeckleClient, stream: Stream):
invite: PendingStreamCollaborator = (
second_client.user.get_all_pending_invites()[0]
second_client.active_user.get_all_pending_invites()[0]
)

accepted = second_client.stream.invite_use(
Expand Down

0 comments on commit cca7b18

Please sign in to comment.