Django Analysis

Protocols

protocol extended_mypy_django_plugin.django_analysis.protocols.Hasher

typing.Protocol.

Classes that implement this protocol must have the following methods / attributes:

__call__(*parts: bytes) str

Given some strings, create a single hash of all that data

protocol extended_mypy_django_plugin.django_analysis.protocols.SettingsTypesDiscovery

Used to discovery the names and types of settings from a loaded project

Classes that implement this protocol must have the following methods / attributes:

__call__(loaded_project: Loaded[T_Project], /) Mapping[str, str]

Call self as a function.

protocol extended_mypy_django_plugin.django_analysis.protocols.InstalledModelsDiscovery

Used to discover installed modules containing Django ORM models in a loaded project

Classes that implement this protocol must have the following methods / attributes:

__call__(loaded_project: Loaded[T_Project], /) Mapping[ImportPath, Module]

Call self as a function.

protocol extended_mypy_django_plugin.django_analysis.protocols.ConcreteModelsDiscovery

Used to determine concrete models

Classes that implement this protocol must have the following methods / attributes:

__call__(loaded_project: Loaded[T_Project], all_models: Mapping[ImportPath, Model], /) Mapping[ImportPath, Sequence[Model]]

Call self as a function.

protocol extended_mypy_django_plugin.django_analysis.protocols.Discovery

A container for all the different discovery helpers

Classes that implement this protocol must have the following methods / attributes:

property discover_settings_types: SettingsTypesDiscovery[T_Project]

Used to discover settings names and their types

property discover_installed_models: InstalledModelsDiscovery[T_Project]

Used to discover installed modules containing Django ORM models

property discover_concrete_models: ConcreteModelsDiscovery[T_Project]

Used to determine the concrete models for any model

protocol extended_mypy_django_plugin.django_analysis.protocols.Project

Represents a Django project to be analyzed

Classes that implement this protocol must have the following methods / attributes:

property root_dir: Path

Where the django project lives

property additional_sys_path: Sequence[str]

Any additional paths that need to be added to sys.path

property env_vars: Mapping[str, str]

Any additional environment variables needed to setup Django

setup_sys_path_and_env_vars() Iterator[None]

Do necessary work to setup and cleanup changes to sys.path and os.environ to prepare for a django instantiation.

load_project() Loaded[Self]

Do necessary work to load Django into memory

It is expected that an implementation will use self.setup_sys_path_and_env_vars to setup and cleanup required changes to sys.path and os.environ

protocol extended_mypy_django_plugin.django_analysis.protocols.Loaded

Represents a Django project that has been setup and loaded into memory

Classes that implement this protocol must have the following methods / attributes:

property project: T_Project

The project that was loaded

property root_dir: Path

Where the django project lives

property env_vars: Mapping[str, str]

Any additional environment variables that were used during Django setup

property settings: LazySettings

The instantiated Django settings object

property apps: Apps

The instantiated Django apps registry

perform_discovery() Discovered[T_Project]

Perform discovery of important information from the loaded Django project

protocol extended_mypy_django_plugin.django_analysis.protocols.Discovered

typing.Protocol.

Classes that implement this protocol must have the following methods / attributes:

property loaded_project: Loaded[T_Project]

The loaded django project that was analyzed

property installed_models_modules: Mapping[ImportPath, Module]

The known modules that contain installed Django Models

property all_models: Mapping[ImportPath, Model]

A map of all the models in the project

property installed_apps: list[str]

The value of the settings.INSTALLED_APPS setting.

property settings_types: Mapping[str, str]

All the django settings and a string representation of their type

property concrete_models: Mapping[ImportPath, Sequence[Model]]

The map of models to their concrete equivalent

protocol extended_mypy_django_plugin.django_analysis.protocols.Module

The models contained within a specific module

Classes that implement this protocol must have the following methods / attributes:

property import_path: ImportPath

The full import path for this module

property defined_models: Mapping[ImportPath, Model]

A map of the installed models defined in this module

protocol extended_mypy_django_plugin.django_analysis.protocols.Model

Represents the information contained by a Django model

Classes that implement this protocol must have the following methods / attributes:

property model_name: str

The name of the class that this model represents

property module_import_path: ImportPath

The import path to the module this model lives in

property import_path: ImportPath

The full import path to this model

property is_abstract: bool

Whether this model is abstract

property default_custom_queryset: ImportPath | None

The import path to the default custom queryset for this model if one is defined

property all_fields: Mapping[str, Field]

The fields associated with this model

property models_in_mro: Sequence[ImportPath]

The import paths to the classes in the mro for this model

As long as they: * aren’t this model * not django.db.models.Model * are subclasses of django.db.models.Model

protocol extended_mypy_django_plugin.django_analysis.protocols.Field

Represents a single field on a model

Classes that implement this protocol must have the following methods / attributes:

property model_import_path: ImportPath

The import path to the model this field is defined on

property field_type: ImportPath

The import path to the type of object used to represent this type

property related_model: ImportPath | None

The model that is related to this field if the field represents a relationship

protocol extended_mypy_django_plugin.django_analysis.protocols.VirtualDependencyNamer

typing.Protocol.

Classes that implement this protocol must have the following methods / attributes:

property namespace: ImportPath

The namespace for the virtual dependencies

__call__(module: ImportPath, /) ImportPath

Return a deterministically determined name representing this module import path

protocol extended_mypy_django_plugin.django_analysis.protocols.VirtualDependencyMaker

Responsible for generating a virtual dependency

Classes that implement this protocol must have the following methods / attributes:

__call__(*, discovered_project: Discovered[T_Project], module: Module) T_CO_VirtualDependency

Call self as a function.

protocol extended_mypy_django_plugin.django_analysis.protocols.VirtualDependencyGenerator

Object that generates the objects representing the virtual dependencies

Classes that implement this protocol must have the following methods / attributes:

__call__(*, discovered_project: Discovered[T_Project]) Mapping[ImportPath, T_CO_VirtualDependency]

Generate the virtual dependencies for this project

protocol extended_mypy_django_plugin.django_analysis.protocols.VirtualDependencyInstaller

Used Install the virtual dependencies into their destination.

Implementations should:

  • Build the reports in the scratch root before installing in the final destination

  • Clear out reports in the final destination that represent modules that don’t exist anymore

Classes that implement this protocol must have the following methods / attributes:

__call__(*, scratch_root: Path, destination: Path, virtual_namespace: ImportPath, report_factory: ReportFactory[T_CO_VirtualDependency, T_Report]) CombinedReport[T_Report]

Call self as a function.

protocol extended_mypy_django_plugin.django_analysis.protocols.VirtualDependencySummary

Represents the different hashes that make up a full hash for a virtual dependency

This is used to determine if the dependency has changed or not

Classes that implement this protocol must have the following methods / attributes:

property virtual_namespace: ImportPath

The import path of the virtual dependencies

property virtual_import_path: ImportPath

The import path the virtual dependency lives at

property module_import_path: ImportPath

The import path to the real module this virtual dependency represents

property significant_info: Sequence[str]

Strings representing significant information related to the module

The idea is that changes in this list should warrant reloading Django in a fresh environment.

protocol extended_mypy_django_plugin.django_analysis.protocols.VirtualDependency

Represents the information held by a virtual dependency for a module

Classes that implement this protocol must have the following methods / attributes:

property module: Module

The module represented by this virtual dependency

property summary: VirtualDependencySummary

The parts that make up a final hash for this virtual dependency

All the models that are related to the module represented by this virtual dependency

property concrete_models: Mapping[ImportPath, Sequence[Model]]

The models known by this module and their concrete children

protocol extended_mypy_django_plugin.django_analysis.protocols.Report

A report of information that can be used by the mypy plugin to easily get information from the virtual dependencies

Classes that implement this protocol must have the following methods / attributes:

register_module(*, module_import_path: ImportPath, virtual_import_path: ImportPath) None

Register a module to it’s virtual path

register_model(*, model_import_path: ImportPath, virtual_import_path: ImportPath, concrete_name: str, concrete_queryset_name: str, concrete_models: Sequence[Model]) None

Register details about a model

protocol extended_mypy_django_plugin.django_analysis.protocols.CombinedReport

typing.Protocol.

Classes that implement this protocol must have the following methods / attributes:

property version: str

Used to determine a string that represents the state of the report such that the string changing indicates the state of the project changed

property report: T_CO_ReportUse

The final combined report

ensure_virtual_dependency(*, module_import_path: str) None

Ensure this module has a virtual dependency if it should have one

This is important because if a module contains models but isn’t installed then when it is added to settings.INSTALLED_APPS without any changes, then we can’t retrospectively give it a virtual dependency in get_additional_deps

protocol extended_mypy_django_plugin.django_analysis.protocols.ReportMaker

Used to construct a report

Classes that implement this protocol must have the following methods / attributes:

__call__() T_CO_Report

Call self as a function.

protocol extended_mypy_django_plugin.django_analysis.protocols.RenderedVirtualDependency

typing.Protocol.

Classes that implement this protocol must have the following methods / attributes:

property content: str

The string representing the content of the virtual dependency

property summary_hash: str | None

A hash representing the state of the dependency

This should be None if the module is not installed

property report: T_CO_Report

The report representing the information in the content

property virtual_import_path: ImportPath

The import path to this virtual dependency

protocol extended_mypy_django_plugin.django_analysis.protocols.VirtualDependencyScribe

Used to render the on disk representation of a virtual dependency along with the information contained in that dependency

Classes that implement this protocol must have the following methods / attributes:

__call__(*, virtual_dependency: T_COT_VirtualDependency, all_virtual_dependencies: Mapping[ImportPath, T_COT_VirtualDependency]) RenderedVirtualDependency[T_CO_Report]

Call self as a function.

protocol extended_mypy_django_plugin.django_analysis.protocols.MakeEmptyVirtualDepContent

Used to make the content that would go into a virtual dependency for an uninstalled module

Classes that implement this protocol must have the following methods / attributes:

__call__(*, module_import_path: ImportPath) str

Call self as a function.

protocol extended_mypy_django_plugin.django_analysis.protocols.EmptyVirtualDepWriter

Used to generate the on disk representation of a virtual dependency for a module that has been determined as an uninstalled module with django models in it.

It should return the import path for the created virtual import, or None if there was an existing virtual dependency or none was made

Classes that implement this protocol must have the following methods / attributes:

__call__(*, module_import_path: ImportPath) ImportPath | None

Call self as a function.

protocol extended_mypy_django_plugin.django_analysis.protocols.ReportInstaller

Used to write reports to the file system

Classes that implement this protocol must have the following methods / attributes:

write_report(*, scratch_root: Path, virtual_import_path: ImportPath, content: str, summary_hash: str | Literal[False] | None) bool

Write a single report to the scratch path

If summary_hash is False, then only write if the virtual dep doesn’t already exist

Return whether a report was written

install_reports(*, scratch_root: Path, destination: Path, virtual_namespace: ImportPath) None

Copy reports from scratch_root into the destination when the reports on the destination are different to the reports in the scratch path.

Also, delete redundant reports from destination

protocol extended_mypy_django_plugin.django_analysis.protocols.ReportCombiner

Used to combine many reports into one

Classes that implement this protocol must have the following methods / attributes:

property reports: Sequence[T_CO_Report]

The reports to combine

combine(*, version: str, write_empty_virtual_dep: EmptyVirtualDepWriter) CombinedReport[T_CO_Report]

Return a single report that represents all the provided reports as one

protocol extended_mypy_django_plugin.django_analysis.protocols.ReportCombinerMaker

Used to create a report combiner

Classes that implement this protocol must have the following methods / attributes:

__call__(*, reports: Sequence[T_Report]) ReportCombiner[T_Report]

Call self as a function.

protocol extended_mypy_django_plugin.django_analysis.protocols.ReportFactory

Holds everything necessary for creating reports

Classes that implement this protocol must have the following methods / attributes:

property report_installer: ReportInstaller
property report_maker: ReportMaker[T_Report]
property report_combiner_maker: ReportCombinerMaker[T_Report]
property make_empty_virtual_dependency_content: MakeEmptyVirtualDepContent
deploy_scribes(virtual_dependencies: Mapping[ImportPath, T_COT_VirtualDependency]) Iterator[RenderedVirtualDependency[T_Report]]
determine_version(*, destination: Path, virtual_namespace: ImportPath, project_version: str, written_dependencies: Sequence[RenderedVirtualDependency[T_Report]]) str

Determine a version for the project, such that it only changes if the mypy daemon needs to be restarted

protocol extended_mypy_django_plugin.django_analysis.protocols.VirtualDependencyHandler

This is the interface required by the mypy plugin to create virtual dependencies and get a report of the information held by those virtual dependencies

Classes that implement this protocol must have the following methods / attributes:

__call__(*, project_root: Path, django_settings_module: str, virtual_deps_destination: Path) CombinedReport[T_CO_ReportUse]

Call self as a function.

Virtual Dependencies

The documentation here still needs work and isn’t better than reading the source for extended_mypy_django_plugin.django_analysis.virtual_dependencies