Skip to content

Commit 9306165

Browse files
committed
fix(type): fix types
1 parent dc18468 commit 9306165

File tree

9 files changed

+43
-46
lines changed

9 files changed

+43
-46
lines changed

.zed/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"languages": {
33
"Python": {
4-
"language_servers": ["pyrefly", "ruff", "!pyright", "!pylsp"]
4+
"language_servers": ["ruff", "pyrefly"]
55
}
66
},
77
"lsp": {

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ unfixable = []
102102

103103
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
104104

105+
[tool.pyrefly]
106+
project-includes = [
107+
"**/*.py*",
108+
"**/*.ipynb",
109+
]
110+
105111
[tool.ruff.format]
106112
quote-style = "double"
107113
indent-style = "space"

src/apmodel/_core/_initial/_rebuild.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List
1+
from typing import List, Type
22

33
from pyld.jsonld import warnings
44
from typing_extensions import Literal, Dict, Any
@@ -76,7 +76,7 @@
7676
from ...vocab.profile import Profile # noqa: F401
7777
from ...vocab.tombstone import Tombstone # noqa: F401
7878

79-
models_to_rebuild: List[ActivityPubModel] = [
79+
models_to_rebuild: List[Type[ActivityPubModel]] = [
8080
# Core
8181
Object,
8282
Activity,
@@ -141,23 +141,14 @@
141141
CryptographicKey,
142142
DataIntegrityProof,
143143
Multikey,
144-
# Nodeinfo
145-
Nodeinfo,
146-
NodeinfoInbound,
147-
NodeinfoOutbound,
148-
NodeinfoProtocol,
149-
NodeinfoServices,
150-
NodeinfoSoftware,
151-
NodeinfoUsage,
152-
NodeinfoUsageUsers,
153144
]
154145

155146

156-
for model in models_to_rebuild:
157-
if isinstance(model, type) and issubclass(model, BaseModel):
147+
for model_cls in models_to_rebuild:
148+
if issubclass(model_cls, BaseModel):
158149
try:
159-
model.model_rebuild()
150+
model_cls.model_rebuild()
160151
except Exception as e:
161-
warnings.warn(f"Failed to rebuild {model.__name__}: {e}")
152+
warnings.warn(f"Failed to rebuild {model_cls.__name__}: {e}")
162153
else:
163154
continue

src/apmodel/_core/key.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def _encode_public_key_as_multibase(
7070
format=serialization.PublicFormat.Raw,
7171
),
7272
)
73+
else:
74+
raise ValueError(f"Unsupported public key type: {type(k)}")
7375
return multibase.encode(wrapped, "base58btc")
7476

7577

@@ -94,6 +96,8 @@ def _encode_private_key_as_multibase(
9496
encryption_algorithm=serialization.NoEncryption(),
9597
),
9698
)
99+
else:
100+
raise ValueError(f"Unsupported private key type: {type(k)}")
97101
return multibase.encode(wrapped, "base58btc")
98102

99103

src/apmodel/context.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ def __repr__(self) -> str:
8585
def __len__(self) -> int:
8686
return len(self.full_context)
8787

88-
def __iter__(self) -> Iterator[str | Dict[str, Any]]:
89-
return iter(self.full_context)
90-
9188
def __getitem__(self, key: Any) -> Any:
9289
return self.full_context[key]
9390

src/apmodel/core/object.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222

2323
class Object(ActivityPubModel):
2424
context: LDContext = Field(
25-
default_factory=lambda: LDContext(
26-
["https://www.w3.org/ns/activitystreams"]
27-
),
25+
default_factory=lambda: LDContext(["https://www.w3.org/ns/activitystreams"]),
2826
kw_only=True,
2927
alias="@context",
3028
)
@@ -36,12 +34,10 @@ class Object(ActivityPubModel):
3634
url: Optional["str | Link"] = Field(default=None)
3735
published: Optional[str] = Field(default=None)
3836
updated: Optional[str] = Field(default=None)
39-
attributed_to: Optional["str | Actor | List[str | Actor]"] = Field(
37+
attributed_to: Optional["str | Actor | List[str | Actor]"] = Field(default=None)
38+
audience: Optional["str | Object | Dict[str, Any] | List[str | Object]"] = Field(
4039
default=None
4140
)
42-
audience: Optional["str | Object | Dict[str, Any] | List[str | Object]"] = (
43-
Field(default=None)
44-
)
4541
to: Optional[
4642
"str | Object | Dict[str, Any] | List[str | Object | Dict[str, Any]]"
4743
] = Field(default=None)
@@ -64,9 +60,7 @@ class Object(ActivityPubModel):
6460
likes: Optional["Collection"] = Field(default=None)
6561
shares: Optional["Collection"] = Field(default=None)
6662
scope: "Optional[Object | Dict[str, Any]]" = Field(default=None)
67-
tag: "List[Object | Hashtag | Emoji | Dict[str, Any]]" = Field(
68-
default_factory=list
69-
)
63+
tag: "List[Object | Hashtag | Emoji | Dict[str, Any]]" = Field(default_factory=list)
7064
attachment: "List[PropertyValue | Dict[str, Any] | Object | Link]" = Field(
7165
default_factory=list
7266
)
@@ -77,9 +71,7 @@ def _convert_field_to_model(cls, v: Any, info: ValidationInfo) -> Any:
7771

7872
if v is None:
7973
return None
80-
parent_context = (
81-
info.context.get("ld_context") if info.context else None
82-
)
74+
parent_context = info.context.get("ld_context") if info.context else None
8375
return load(v, "raw", parent_context=parent_context)
8476

8577
@field_validator(

src/apmodel/loader.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from apmodel.context import LDContext
88

99
from ._core._jsonjd.loader import create_document_loader
10-
from .nodeinfo import Nodeinfo
1110
from .registry import registry
1211
from .types import ActivityPubModel
1312

@@ -66,9 +65,6 @@ def load(
6665
f"with data {data_to_validate}: {e}"
6766
)
6867

69-
if Nodeinfo.is_nodeinfo_data(data_to_validate):
70-
return Nodeinfo.from_json(data_to_validate)
71-
7268
if default == "raw":
7369
return data
7470
return default

tests/test_real_data.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ def test_misskey_person(test_data_path: Path):
3535
assert isinstance(actor.attachment[0], PropertyValue)
3636

3737
# check extra value
38-
assert actor._misskey_summary == "Hello"
38+
assert actor.model_extra is not None
39+
assert actor.model_extra.get("_misskey_summary") == "Hello"
3940

4041
# check apmodel methods
42+
assert actor.public_key is not None
4143
assert isinstance(actor.public_key.public_key, RSAPublicKey)
4244

4345

@@ -84,14 +86,17 @@ def test_fedibird_person(test_data_path: Path):
8486
assert isinstance(actor.attachment[0], PropertyValue)
8587

8688
# check extra value
89+
assert actor.model_extra is not None
8790
assert actor.model_extra.get("vcard:Address") == "Earth"
88-
assert isinstance(actor.otherSetting, list)
91+
assert isinstance(actor.model_extra.get("otherSetting"), list)
92+
searchable_by = actor.model_extra.get("searchableBy")
8993
assert (
90-
isinstance(actor.searchableBy, list)
91-
and actor.searchableBy[0] == "https://fedibird.example.com/users/user"
94+
isinstance(searchable_by, list)
95+
and searchable_by[0] == "https://fedibird.example.com/users/user"
9296
)
9397

9498
# check apmodel methods
99+
assert actor.public_key is not None
95100
assert isinstance(actor.public_key.public_key, RSAPublicKey)
96101

97102

@@ -107,8 +112,9 @@ def test_akkoma_actor(test_data_path: Path):
107112
assert actor.name == "User"
108113
assert actor.summary == "Hello"
109114
assert isinstance(actor.endpoints, ActorEndpoints)
115+
assert actor.endpoints.model_extra is not None
110116
assert (
111-
actor.endpoints.oauthAuthorizationEndpoint
117+
actor.endpoints.model_extra.get("oauthAuthorizationEndpoint")
112118
== "https://akkoma.example.com/oauth/authorize"
113119
)
114120

@@ -124,7 +130,8 @@ def test_akkoma_note(test_data_path: Path):
124130
note.id
125131
== "https://akkoma.example.com/objects/bcd39b5c-004f-4897-81b1-2b7a4caa398b"
126132
)
127-
assert note.sensitive is True
133+
assert note.model_extra is not None
134+
assert note.model_extra.get("sensitive") is True
128135
assert note.summary == "test"
129136
assert note.content == "<p>:blobthumbsup: 👀</p>"
130137

@@ -149,6 +156,7 @@ def test_mastodon_actor(test_data_path: Path):
149156
assert actor.endpoints.shared_inbox == "https://mastodon.example.com/inbox"
150157
assert actor.discoverable is True
151158
assert actor.indexable is True
159+
assert actor.public_key is not None
152160
assert isinstance(actor.public_key.public_key, RSAPublicKey)
153161
assert actor.public_key.id == "https://mastodon.example.com/users/user#main-key"
154162
assert actor.public_key.owner == "https://mastodon.example.com/users/user"
@@ -168,15 +176,16 @@ def test_mastodon_note(test_data_path: Path):
168176
== "https://mastodon.example.com/users/user/statuses/2002577998669970637"
169177
)
170178
assert note.content == "<p>Hello!</p>"
171-
assert note.sensitive is False
179+
assert note.model_extra is not None
180+
assert note.model_extra.get("sensitive") is False
172181
assert note.attributed_to == "https://mastodon.example.com/users/user"
173182
assert isinstance(note.replies, Collection)
174183
assert isinstance(note.likes, Collection)
175184
assert isinstance(note.shares, Collection)
176185

177186

178187
def test_akkoma_replies(test_data_path: Path):
179-
from apmodel.core.collection import OrderedCollection
188+
from apmodel.core.collection import OrderedCollection, OrderedCollectionPage
180189

181190
data_loc = test_data_path / "akkoma_replies.json"
182191
with open(data_loc, "r") as f:
@@ -186,4 +195,5 @@ def test_akkoma_replies(test_data_path: Path):
186195
assert isinstance(replies, OrderedCollection)
187196
assert replies.total_items == 1
188197
assert replies.first is not None
198+
assert isinstance(replies.first, OrderedCollectionPage)
189199
assert replies.first.type == "OrderedCollectionPage"

tests/test_registry.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from typing import Dict, Optional, Type, Union
23

34
import pytest
45
from pydantic import Field
@@ -7,7 +8,7 @@
78
from apmodel.registry import ModelRegistry, _load_model_cls
89
from apmodel.types import ActivityPubModel
910

10-
MINIMAL_PRELOADS = {
11+
MINIMAL_PRELOADS: Dict[str, Union[str, Type[ActivityPubModel]]] = {
1112
"https://www.w3.org/ns/activitystreams#Object": "apmodel.core.object.Object",
1213
"https://www.w3.org/ns/activitystreams#Person": "apmodel.vocab.actor.Person",
1314
}
@@ -54,7 +55,7 @@ def test_register_custom_model(test_registry: ModelRegistry):
5455
"""Tests registration of a new custom model."""
5556

5657
class CustomObject(Object):
57-
type: str = Field("https://example.com/ns#CustomObject", frozen=True)
58+
type: Optional[str] = Field("https://example.com/ns#CustomObject", frozen=True)
5859

5960
test_registry.register(CustomObject, "https://example.com/ns#CustomObject")
6061
retrieved_class = test_registry.get("https://example.com/ns#CustomObject")

0 commit comments

Comments
 (0)