Skip to content

Commit

Permalink
Rewrite all uses of compile(x) to x._compile()
Browse files Browse the repository at this point in the history
This was done using:

    find . -name '*.py' | xargs sed -i -r -e 's/ compile\((\w+)\)/ \1._compile()/g'

And then unnecessary imports were removed using:

    just fix
  • Loading branch information
evansd committed Dec 4, 2024
1 parent f5352fa commit 43a6a6b
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 42 deletions.
4 changes: 2 additions & 2 deletions ehrql/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import ehrql
from ehrql.debugger import activate_debug_context
from ehrql.measures import Measures
from ehrql.query_language import Dataset, compile, modify_exception
from ehrql.query_language import Dataset, modify_exception
from ehrql.renderers import DISPLAY_RENDERERS
from ehrql.serializer import deserialize
from ehrql.utils.traceback_utils import get_trimmed_traceback
Expand Down Expand Up @@ -282,7 +282,7 @@ def get_variable_definitions_from_module(module):
raise DefinitionError(
"A population has not been defined; define one with define_population()"
)
return compile(dataset)
return dataset._compile()


def load_measure_definitions_unsafe(definition_file, user_args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions ehrql/query_engines/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from ehrql.query_engines.in_memory_database import apply_function
from ehrql.query_engines.local_file import LocalFileQueryEngine
from ehrql.query_language import Dataset, DateDifference, compile
from ehrql.query_language import Dataset, DateDifference
from ehrql.query_model.introspection import get_table_nodes
from ehrql.query_model.nodes import AggregateByPatient, Function


class SandboxQueryEngine(LocalFileQueryEngine):
def evaluate_dataset(self, dataset_definition):
variable_definitions = compile(dataset_definition)
variable_definitions = dataset_definition._compile()
if not variable_definitions:
return EmptyDataset()
table_nodes = get_table_nodes(*variable_definitions.values())
Expand Down
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from ehrql.query_engines.mssql import MSSQLQueryEngine
from ehrql.query_engines.sqlite import SQLiteQueryEngine
from ehrql.query_engines.trino import TrinoQueryEngine
from ehrql.query_language import compile
from tests.lib.orm_utils import make_orm_models

from .lib.databases import (
Expand Down Expand Up @@ -196,7 +195,7 @@ def query_engine(self, dsn=False, **engine_kwargs):
return self.query_engine_class(dsn, **engine_kwargs)

def extract(self, dataset, **engine_kwargs):
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
return self.extract_qm(variable_definitions, **engine_kwargs)

def extract_qm(self, variable_definitions, **engine_kwargs):
Expand All @@ -207,7 +206,7 @@ def extract_qm(self, variable_definitions, **engine_kwargs):
return [row._asdict() for row in sorted(results)]

def dump_dataset_sql(self, dataset, **engine_kwargs):
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
query_engine = self.query_engine(dsn=None, **engine_kwargs)
return get_sql_strings(query_engine, variable_definitions)

Expand Down
5 changes: 2 additions & 3 deletions tests/integration/backends/test_emis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from ehrql import create_dataset
from ehrql.backends.emis import EMISBackend
from ehrql.query_engines.base_sql import get_setup_and_cleanup_queries
from ehrql.query_language import compile
from ehrql.tables import PatientFrame, Series, emis, table_from_rows
from ehrql.tables.raw import emis as emis_raw
from ehrql.utils.sqlalchemy_query_utils import CreateTableAs, GeneratedTable
Expand Down Expand Up @@ -606,7 +605,7 @@ class t(PatientFrame):
backend=backend,
)

variables = compile(dataset)
variables = dataset._compile()

results_query = query_engine.get_query(variables)
inline_tables = [
Expand Down Expand Up @@ -662,7 +661,7 @@ def test_temp_table_includes_organisation_hash(trino_database):
backend=backend,
)

variables = compile(dataset)
variables = dataset._compile()
results_query = query_engine.get_query(variables)
temp_tables = [
ch
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/query_engines/test_local_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from ehrql import Dataset
from ehrql.query_engines.local_file import LocalFileQueryEngine
from ehrql.query_language import compile
from ehrql.tables import EventFrame, PatientFrame, Series, table


Expand Down Expand Up @@ -31,7 +30,7 @@ def test_local_file_query_engine():
).count_for_patient()

dataset.define_population(patients.exists_for_patient())
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()

query_engine = LocalFileQueryEngine(FIXTURES)
results = query_engine.get_results(variable_definitions)
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/dummy_data/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ehrql import Dataset
from ehrql.dummy_data.generator import DummyDataGenerator, DummyPatientGenerator
from ehrql.dummy_data.query_info import ColumnInfo, TableInfo
from ehrql.query_language import compile, table_from_rows
from ehrql.query_language import table_from_rows
from ehrql.tables import Constraint, EventFrame, PatientFrame, Series, table


Expand Down Expand Up @@ -70,7 +70,7 @@ def test_dummy_data_generator():
dataset.date = last_event.date

# Generate some results
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyDataGenerator(variable_definitions)
generator.population_size = 7
generator.batch_size = 4
Expand All @@ -97,7 +97,7 @@ def test_dummy_data_generator_timeout_with_some_results(patched_time):
dataset = Dataset()
dataset.define_population(patients.exists_for_patient())

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyDataGenerator(variable_definitions)
generator.population_size = 100
generator.batch_size = 3
Expand All @@ -120,7 +120,7 @@ def test_dummy_data_generator_timeout_with_no_results(patched_time):
dataset = Dataset()
dataset.define_population(patients.sex != patients.sex)

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyDataGenerator(variable_definitions)
generator.timeout = 10

Expand Down Expand Up @@ -180,7 +180,7 @@ class inline_table(PatientFrame):
)

# Generate some results
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyDataGenerator(variable_definitions)
# We're asking for more results than we can possibly get (because there are only 6
# patients in the inline table). We expect the attempt to timeout and just return 6
Expand Down Expand Up @@ -277,7 +277,7 @@ def test_get_random_int_with_range(dummy_patient_generator):
def dummy_patient_generator():
dataset = Dataset()
dataset.define_population(patients.exists_for_patient())
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyPatientGenerator(
variable_definitions,
random_seed="abc",
Expand Down
9 changes: 4 additions & 5 deletions tests/unit/dummy_data/test_query_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from ehrql import Dataset, days
from ehrql.codes import CTV3Code
from ehrql.dummy_data.query_info import ColumnInfo, QueryInfo, TableInfo
from ehrql.query_language import compile
from ehrql.tables import (
Constraint,
EventFrame,
Expand Down Expand Up @@ -42,7 +41,7 @@ def test_query_info_from_variable_definitions():
events.code == CTV3Code("abc00")
).exists_for_patient()

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
query_info = QueryInfo.from_variable_definitions(variable_definitions)

assert query_info == QueryInfo(
Expand Down Expand Up @@ -99,7 +98,7 @@ class test_table(PatientFrame):
| test_table.value.contains("d")
)

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
query_info = QueryInfo.from_variable_definitions(variable_definitions)
column_info = query_info.tables["test_table"].columns["value"]

Expand Down Expand Up @@ -127,7 +126,7 @@ class inline_table(PatientFrame):
| inline_table.value.contains("d")
)

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
query_info = QueryInfo.from_variable_definitions(variable_definitions)

assert query_info == QueryInfo(
Expand All @@ -150,7 +149,7 @@ def test_query_info_ignores_complex_comparisons():
dataset.q1 = patients.date_of_birth.year.is_in([2000, 2010, 2020])
dataset.q2 = patients.date_of_birth + days(100) == "2021-10-20"
dataset.q3 = patients.date_of_birth == "2022-10-05"
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()

query_info = QueryInfo.from_variable_definitions(variable_definitions)
column_info = query_info.tables["patients"].columns["date_of_birth"]
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/dummy_data_nextgen/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ehrql import Dataset
from ehrql.dummy_data_nextgen.generator import DummyDataGenerator, DummyPatientGenerator
from ehrql.dummy_data_nextgen.query_info import ColumnInfo, TableInfo
from ehrql.query_language import compile, table_from_rows
from ehrql.query_language import table_from_rows
from ehrql.tables import Constraint, EventFrame, PatientFrame, Series, table


Expand Down Expand Up @@ -76,7 +76,7 @@ def test_dummy_data_generator():
# Generate some results
target_size = 7

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyDataGenerator(variable_definitions, population_size=target_size)
generator.batch_size = 4
results = list(generator.get_results())
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_dummy_data_generator_timeout_with_some_results(patched_time):
dataset = Dataset()
dataset.define_population(patients.exists_for_patient())

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyDataGenerator(variable_definitions)
generator.population_size = 100
generator.batch_size = 3
Expand All @@ -132,7 +132,7 @@ def test_dummy_data_generator_timeout_with_no_results(patched_time):
& (patients.date_of_death.day == 2)
)

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyDataGenerator(variable_definitions)
generator.timeout = 10

Expand Down Expand Up @@ -192,7 +192,7 @@ class inline_table(PatientFrame):
)

# Generate some results
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyDataGenerator(variable_definitions)
# We're asking for more results than we can possibly get (because there are only 6
# patients in the inline table). We expect the attempt to timeout and just return 6
Expand Down Expand Up @@ -336,7 +336,7 @@ def test_cannot_generate_data_outside_of_a_seed_block(dummy_patient_generator):
def dummy_patient_generator():
dataset = Dataset()
dataset.define_population(patients.exists_for_patient())
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyPatientGenerator(
variable_definitions,
random_seed="abc",
Expand Down
9 changes: 4 additions & 5 deletions tests/unit/dummy_data_nextgen/test_query_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from ehrql import Dataset, days
from ehrql.codes import CTV3Code
from ehrql.dummy_data_nextgen.query_info import ColumnInfo, QueryInfo, TableInfo
from ehrql.query_language import compile
from ehrql.tables import (
Constraint,
EventFrame,
Expand Down Expand Up @@ -42,7 +41,7 @@ def test_query_info_from_variable_definitions():
events.code == CTV3Code("abc00")
).exists_for_patient()

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
query_info = QueryInfo.from_variable_definitions(variable_definitions)

assert query_info == QueryInfo(
Expand Down Expand Up @@ -99,7 +98,7 @@ class test_table(PatientFrame):
| test_table.value.contains("d")
)

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
query_info = QueryInfo.from_variable_definitions(variable_definitions)
column_info = query_info.tables["test_table"].columns["value"]

Expand All @@ -123,7 +122,7 @@ class inline_table(PatientFrame):
| inline_table.value.contains("d")
)

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
query_info = QueryInfo.from_variable_definitions(variable_definitions)

assert query_info == QueryInfo(
Expand All @@ -146,7 +145,7 @@ def test_query_info_ignores_complex_comparisons():
dataset.q1 = patients.date_of_birth.year.is_in([2000, 2010, 2020])
dataset.q2 = patients.date_of_birth + days(100) == "2021-10-20"
dataset.q3 = patients.date_of_birth == "2022-10-05"
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()

query_info = QueryInfo.from_variable_definitions(variable_definitions)
column_info = query_info.tables["patients"].columns["date_of_birth"]
Expand Down
13 changes: 6 additions & 7 deletions tests/unit/dummy_data_nextgen/test_specific_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
EventFrame,
PatientFrame,
Series,
compile,
table,
table_from_rows,
)
Expand Down Expand Up @@ -68,7 +67,7 @@ def test_queries_with_exact_one_shot_generation(patched_time, query):

target_size = 1000

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyDataGenerator(variable_definitions, population_size=target_size)
generator.batch_size = target_size
generator.timeout = 10
Expand Down Expand Up @@ -138,7 +137,7 @@ def test_combined_age_range_in_one_shot(patched_time, query, target_size):

dataset.define_population(query)

variable_definitions = compile(dataset)
variable_definitions = dataset._compile()
generator = DummyDataGenerator(variable_definitions, population_size=target_size)
generator.batch_size = target_size
generator.timeout = 10
Expand Down Expand Up @@ -221,7 +220,7 @@ def test_queries_not_yet_well_handled(patched_time, query):
dataset.define_population(patients.exists_for_patient() & query)

target_size = 1000
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()

generator = DummyDataGenerator(variable_definitions, population_size=target_size)
generator.batch_size = target_size
Expand Down Expand Up @@ -251,7 +250,7 @@ def test_inline_table_query(patched_time):
dataset.i = p.i

target_size = 1000
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()

generator = DummyDataGenerator(variable_definitions, population_size=target_size)
generator.batch_size = target_size
Expand Down Expand Up @@ -284,7 +283,7 @@ def test_will_raise_if_all_data_is_impossible(patched_time, query):

dataset.define_population(query)
target_size = 1000
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()

generator = DummyDataGenerator(variable_definitions, population_size=target_size)
generator.timeout = 1
Expand All @@ -309,7 +308,7 @@ def test_generates_events_starting_from_birthdate():

target_size = 1000
dataset.configure_dummy_data(population_size=target_size)
variable_definitions = compile(dataset)
variable_definitions = dataset._compile()

generator = DummyDataGenerator(variable_definitions, population_size=target_size)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_query_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_dataset():
assert dataset.year_of_birth is year_of_birth
assert dataset.dummy_data_config.population_size == 123

assert compile(dataset) == {
assert dataset._compile() == {
"year_of_birth": Function.YearFromDate(
source=SelectColumn(
name="date_of_birth",
Expand Down

0 comments on commit 43a6a6b

Please sign in to comment.