Spaces:
Sleeping
Sleeping
File size: 5,824 Bytes
feb1b1c e776c3c feb1b1c 830d137 feb1b1c 830d137 feb1b1c e776c3c feb1b1c e776c3c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
from __future__ import annotations
from collections.abc import Mapping
from datetime import date
from types import MappingProxyType
from typing import Any, final
from pydantic import (
BaseModel,
ConfigDict,
Field,
ValidationError,
field_serializer,
field_validator,
)
from redstack.domain.enums import (
CompanySize,
InstitutionTier,
LanguageProficiency,
Proficiency,
WorkMode,
)
from redstack.domain.errors import SchemaError
from redstack.domain.ids import CandidateId, LpaAmount, Months, SkillName
_STRICT = ConfigDict(
frozen=True, extra="forbid", str_strip_whitespace=True, validate_default=True
)
@final
class RawProfile(BaseModel):
"""Top-level profile facts."""
model_config = _STRICT
anonymized_name: str = Field(min_length=1)
headline: str
summary: str
location: str
country: str
years_of_experience: float = Field(ge=0.0, le=50.0, allow_inf_nan=False)
current_title: str
current_company: str
current_company_size: CompanySize
current_industry: str
@final
class RawPosition(BaseModel):
"""One career-history position, mirrored verbatim."""
model_config = _STRICT
company: str
title: str
start_date: date
end_date: date | None
duration_months: Months = Field(ge=0)
is_current: bool
industry: str
company_size: CompanySize
description: str
@final
class RawEducation(BaseModel):
"""One education record."""
model_config = _STRICT
institution: str
degree: str
field_of_study: str
start_year: int
end_year: int
grade: str | None
tier: InstitutionTier
@final
class RawSkill(BaseModel):
"""One claimed skill."""
model_config = _STRICT
name: SkillName
proficiency: Proficiency
endorsements: int = Field(ge=0)
duration_months: Months | None = Field(default=None, ge=0)
@final
class RawCertification(BaseModel):
"""One certification."""
model_config = _STRICT
name: str
issuer: str
year: int
@final
class RawLanguage(BaseModel):
"""One spoken language."""
model_config = _STRICT
language: str
proficiency: LanguageProficiency
@final
class RawSalaryRange(BaseModel):
"""Expected salary range (INR lpa); inversion preserved, never corrected.
Field names mirror the source JSON verbatim (``{"min": ..., "max": ...}``),
not the ``*_lpa``-suffixed names used by downstream domain models.
"""
model_config = _STRICT
min: LpaAmount = Field(ge=0.0, allow_inf_nan=False)
max: LpaAmount = Field(ge=0.0, allow_inf_nan=False)
@final
class RawSignals(BaseModel):
"""The 23 ``redrob_signals``, typed exactly; sentinels preserved as-is."""
model_config = _STRICT
profile_completeness_score: float = Field(ge=0.0, le=100.0, allow_inf_nan=False)
signup_date: date
last_active_date: date
open_to_work_flag: bool
profile_views_received_30d: int = Field(ge=0)
applications_submitted_30d: int = Field(ge=0)
recruiter_response_rate: float = Field(ge=0.0, le=1.0, allow_inf_nan=False)
avg_response_time_hours: float = Field(ge=0.0, allow_inf_nan=False)
skill_assessment_scores: Mapping[str, float]
connection_count: int = Field(ge=0)
endorsements_received: int = Field(ge=0)
notice_period_days: int = Field(ge=0, le=180)
expected_salary_range_inr_lpa: RawSalaryRange
preferred_work_mode: WorkMode
willing_to_relocate: bool
github_activity_score: float = Field(ge=-1.0, le=100.0, allow_inf_nan=False)
search_appearance_30d: int = Field(ge=0)
saved_by_recruiters_30d: int = Field(ge=0)
interview_completion_rate: float = Field(ge=0.0, le=1.0, allow_inf_nan=False)
offer_acceptance_rate: float = Field(ge=-1.0, le=1.0, allow_inf_nan=False)
verified_email: bool
verified_phone: bool
linkedin_connected: bool
@field_validator("skill_assessment_scores", mode="after")
@classmethod
def _freeze_scores(cls, value: Mapping[str, float]) -> Mapping[str, float]:
for score in value.values():
if not (0.0 <= score <= 100.0):
raise ValueError("skill_assessment_scores out of range [0, 100]")
return MappingProxyType(dict(value))
@field_serializer("skill_assessment_scores")
def _dump_scores(self, value: Mapping[str, float]) -> dict[str, float]:
# ``MappingProxyType`` is not natively serializable; project the
# read-only view back to a plain ``dict`` so ``RawCandidate`` round-trips
# to JSON losslessly (provenance depends on this, §P).
return dict(value)
@final
class RawCandidate(BaseModel):
"""Aggregate of raw facts — the canonical evidence source."""
model_config = _STRICT
candidate_id: CandidateId = Field(pattern=r"^CAND_[0-9]{7}$")
profile: RawProfile
career_history: tuple[RawPosition, ...] = Field(min_length=1, max_length=10)
education: tuple[RawEducation, ...] = Field(max_length=5)
skills: tuple[RawSkill, ...]
certifications: tuple[RawCertification, ...]
languages: tuple[RawLanguage, ...]
redrob_signals: RawSignals
@classmethod
def from_mapping(cls, data: Mapping[str, Any]) -> RawCandidate:
"""Validate and narrow a raw mapping — the sole ``Any`` boundary.
Type/shape violations are re-raised as ``SchemaError``; semantic
contradictions are intentionally preserved for downstream detection.
"""
try:
return cls.model_validate(data)
except ValidationError as exc:
raise SchemaError(str(exc)) from exc
__all__: tuple[str, ...] = (
"RawCandidate",
"RawCertification",
"RawEducation",
"RawLanguage",
"RawPosition",
"RawProfile",
"RawSalaryRange",
"RawSignals",
"RawSkill",
) |