Claims and statements
Wikidata serves statements in two shapes: the Action API shape through client.claims, and the Wikibase REST shape through client.statements. The examples on this page use the client from the first block; later
blocks reuse it.
import asyncio
from bibliofabric.wikidata import WikidataClient
client = WikidataClient()
Get the claims of an entity
client.claims wraps wbgetclaims and returns claims keyed by property id, typed as Claim.
claims = await client.claims.get("Q42", property_id="P569")
print("properties:", list(claims))
claim = claims["P569"][0]
print("rank:", claim.rank)
print("value:", claim.mainsnak.datavalue.value)
properties: ['P569']
rank: normal
value: {'time': '+1952-03-11T00:00:00Z', 'timezone': 0, 'before': 0, 'after': 0, 'precision': 11, 'calendarmodel': 'http://www.wikidata.org/entity/Q1985727'}
property_id= restricts the answer to one property, and rank= filters by rank: "deprecated", "normal", or "preferred". Any other rank value makes the API answer badvalue, so the type rejects an invalid value before the request. An entity without claims answers {}.
Each Claim carries mainsnak, rank, qualifiers (a dict of property id to Snak), and references. Snak.datavalue keeps the value untyped, so every Wikidata value shape survives.
Get the REST statements of an entity
client.statements reads the same statements through the Wikibase REST API, typed as RestStatement. REST values are typed. property_id is a {id, data_type} object and value
is a {type, content} object, with content set to None for somevalue
and novalue snaks.
statements = await client.statements.for_entity("Q42", property_id="P31")
print("properties:", list(statements))
statement = statements["P31"][0]
print("id:", statement.id)
print("value:", statement.property_id.id, statement.value.content)
print(
"qualifiers:",
len(statement.qualifiers or []),
"references:",
len(statement.references or []),
)
properties: ['P31']
id: Q42$F078E5B3-F9A8-480E-B7AC-D97778CBBEF9
value: P31 Q5
qualifiers: 1 references: 3
for_entity() accepts item ids (Q42) and property ids (P31); a lexeme or mediaInfo id raises ConfigurationError, because the REST statements routes serve items and properties only. Pass property_id= to restrict the answer to one property.
Fetch one statement by id
Statement ids look like Q42$F078E5B3-F9A8-480E-B7AC-D97778CBBEF9; the $ is URL-encoded in the request path. An id without a $ raises ConfigurationError.
statement = await client.statements.get("Q42$F078E5B3-F9A8-480E-B7AC-D97778CBBEF9")
print(statement.id, statement.value.content)
Q42$F078E5B3-F9A8-480E-B7AC-D97778CBBEF9 Q5
Which shape to use
The two shapes carry the same facts. The Action API shape gives you the raw Wikidata value objects (Claim.mainsnak.datavalue.value), works for every entity kind including lexemes, and comes back one entity at a time. The REST shape gives you typed values (RestStatement.value.content), and its routes also serve one statement by id, which the Action API does not offer.