Implementation of the least upper bound algorithm. Attributes: s: The other (left) type operand.
| 254 | |
| 255 | |
| 256 | class TypeJoinVisitor(TypeVisitor[ProperType]): |
| 257 | """Implementation of the least upper bound algorithm. |
| 258 | |
| 259 | Attributes: |
| 260 | s: The other (left) type operand. |
| 261 | """ |
| 262 | |
| 263 | def __init__(self, s: ProperType, instance_joiner: InstanceJoiner | None = None) -> None: |
| 264 | self.s = s |
| 265 | self.instance_joiner = instance_joiner |
| 266 | |
| 267 | def visit_unbound_type(self, t: UnboundType) -> ProperType: |
| 268 | return AnyType(TypeOfAny.special_form) |
| 269 | |
| 270 | def visit_union_type(self, t: UnionType) -> ProperType: |
| 271 | if is_proper_subtype(self.s, t): |
| 272 | return t |
| 273 | else: |
| 274 | return mypy.typeops.make_simplified_union([self.s, t]) |
| 275 | |
| 276 | def visit_any(self, t: AnyType) -> ProperType: |
| 277 | return t |
| 278 | |
| 279 | def visit_none_type(self, t: NoneType) -> ProperType: |
| 280 | if state.strict_optional: |
| 281 | if isinstance(self.s, (NoneType, UninhabitedType)): |
| 282 | return t |
| 283 | elif isinstance(self.s, (UnboundType, AnyType)): |
| 284 | return AnyType(TypeOfAny.special_form) |
| 285 | else: |
| 286 | return mypy.typeops.make_simplified_union([self.s, t]) |
| 287 | else: |
| 288 | return self.s |
| 289 | |
| 290 | def visit_uninhabited_type(self, t: UninhabitedType) -> ProperType: |
| 291 | return self.s |
| 292 | |
| 293 | def visit_deleted_type(self, t: DeletedType) -> ProperType: |
| 294 | return self.s |
| 295 | |
| 296 | def visit_erased_type(self, t: ErasedType) -> ProperType: |
| 297 | return self.s |
| 298 | |
| 299 | def visit_type_var(self, t: TypeVarType) -> ProperType: |
| 300 | if isinstance(self.s, TypeVarType): |
| 301 | if self.s.id == t.id: |
| 302 | if self.s.upper_bound == t.upper_bound: |
| 303 | return self.s |
| 304 | return self.s.copy_modified( |
| 305 | upper_bound=join_types(self.s.upper_bound, t.upper_bound) |
| 306 | ) |
| 307 | # Fix non-commutative joins |
| 308 | return get_proper_type(join_types(self.s.upper_bound, t.upper_bound)) |
| 309 | else: |
| 310 | return self.default(self.s) |
| 311 | |
| 312 | def visit_param_spec(self, t: ParamSpecType) -> ProperType: |
| 313 | if self.s == t: |
no outgoing calls
no test coverage detected
searching dependent graphs…