Creates a read-only, self-contained snapshot of a type object. Properties of a snapshot: - Contains (nested) tuples and other immutable primitive objects only. - References to AST nodes are replaced with full names of targets. - Has no references to mutable or non-primitive objects
| 358 | |
| 359 | |
| 360 | class SnapshotTypeVisitor(TypeVisitor[SnapshotItem]): |
| 361 | """Creates a read-only, self-contained snapshot of a type object. |
| 362 | |
| 363 | Properties of a snapshot: |
| 364 | |
| 365 | - Contains (nested) tuples and other immutable primitive objects only. |
| 366 | - References to AST nodes are replaced with full names of targets. |
| 367 | - Has no references to mutable or non-primitive objects. |
| 368 | - Two snapshots represent the same object if and only if they are |
| 369 | equal. |
| 370 | - Results must be sortable. It's important that tuples have |
| 371 | consistent types and can't arbitrarily mix str and None values, |
| 372 | for example, since they can't be compared. |
| 373 | """ |
| 374 | |
| 375 | def visit_unbound_type(self, typ: UnboundType) -> SnapshotItem: |
| 376 | return ( |
| 377 | "UnboundType", |
| 378 | typ.name, |
| 379 | typ.optional, |
| 380 | typ.empty_tuple_index, |
| 381 | snapshot_types(typ.args), |
| 382 | ) |
| 383 | |
| 384 | def visit_any(self, typ: AnyType) -> SnapshotItem: |
| 385 | return snapshot_simple_type(typ) |
| 386 | |
| 387 | def visit_none_type(self, typ: NoneType) -> SnapshotItem: |
| 388 | return snapshot_simple_type(typ) |
| 389 | |
| 390 | def visit_uninhabited_type(self, typ: UninhabitedType) -> SnapshotItem: |
| 391 | return snapshot_simple_type(typ) |
| 392 | |
| 393 | def visit_erased_type(self, typ: ErasedType) -> SnapshotItem: |
| 394 | return snapshot_simple_type(typ) |
| 395 | |
| 396 | def visit_deleted_type(self, typ: DeletedType) -> SnapshotItem: |
| 397 | return snapshot_simple_type(typ) |
| 398 | |
| 399 | def visit_instance(self, typ: Instance) -> SnapshotItem: |
| 400 | extra_attrs: SnapshotItem |
| 401 | if typ.extra_attrs: |
| 402 | extra_attrs = ( |
| 403 | tuple(sorted((k, v.accept(self)) for k, v in typ.extra_attrs.attrs.items())), |
| 404 | tuple(typ.extra_attrs.immutable), |
| 405 | ) |
| 406 | else: |
| 407 | extra_attrs = () |
| 408 | return ( |
| 409 | "Instance", |
| 410 | encode_optional_str(typ.type.fullname), |
| 411 | snapshot_types(typ.args), |
| 412 | ("None",) if typ.last_known_value is None else snapshot_type(typ.last_known_value), |
| 413 | extra_attrs, |
| 414 | ) |
| 415 | |
| 416 | def visit_type_var(self, typ: TypeVarType) -> SnapshotItem: |
| 417 | return ( |
no outgoing calls
no test coverage detected
searching dependent graphs…