Skip to content

Commit

Permalink
Pylance fixes (#906)
Browse files Browse the repository at this point in the history
* add `id: Serial`

* replace `pass` with `... ` in abstract properties

* improve types for Postgres' `AsyncBatch`

* improve types for SQLite's `AsyncBatch`

* ignore deliberate type errors

* add assertions for nullable values

* fix type annotation for `test_function` in `MigrationTestCase`

* more null assertions

* make sure the error code is a string

* add `id: Serial` to a table

* fix iPython import path

* add type annotations to `COLUMN_DEFAULT_PARSER`

* add pyright to CI

* check that engine is `PostgresEngine`

* ignore deliberate type error

* more null assertions

* more null assertions

* fix database checks

* ignore unbound value errors

* make sure `Engine` has `log_queries` and `log_responses` values

* remove test cases which don't match literal values

* null assertions

* ignore deliberate type error

* add `id: Serial` annotations

* fix type annotation for `Time` default value

allow it to receive a function which returns a `time` value

* fix type annotation for `Date` offset

It can accept a function which returns a `date` value

* fix type annotation for `Timestamp` default

* fix remaining errors in `test_migrations.py`

* refactor `test_add_column`

* add test methods to `M2MBase`

* ignore some type errors

* fix pydantic types

* fix type warnings with engines

* fix `fixtures/commands/load.py` warnings

* fix `schema.py` warnings

* fix `table.py` warnings

* make assertion more explicit

* refactor `M2MBase`

* fix `test_m2m.py` warnings

* fixes for `migration_manager.py`

* ignore some errors

* foreign key improvements

* fix formatting

* fixes for `test_join.py`  and `test_objects.py`

* fix remaining issues in `test_join.py`

* ignore deliberate error in `test_slots.py`

* fix issues in `test_all_related.py`

* fix errors in `test_pydantic.py`

* fix errors in `schema.py`

* fix errors in `test_select.py`

* fix errors in `test_get_related_readable.py`

* fix errors in `test_all_columns.py`

* fix errors in `user/test_tables.py`

* fix errors in `test_indexes.py`

* fix errors in `select.py`

* remove unused `Self`

* fix errors with `M2MBase`

* fix errors in `test_foreign_key_self.py`

* fix errors in `test_primary_key.py`

* fix errors with `m2m.py`

* fix errors with `test_objects.py`

* ignore errors with update

* fix migration errors with `migrations_folder_path`

* fix array errors

* added `resolved_migrations_folder_path`

* try fixing `Batch`

* add an extra overload for `output`

* fix errors in `WhereDelegate` and `AddDelegate`

* fix errors in `test_attribute_access.py`

* fix errors in `test_schema.py`

* use `resolved_migrations_folder_path` in `test_apps.py`

* fix errors in `test_columns_delegate.py`

* ignore error in `test_output.py`

* add explicit exports to `migrations/auto/__init__.py`

* improve `atomic`

* disable pyright in linter for now

* reformat with latest black

* remove unused import

* remove duplicate import

* fix test

* rename `Batch` to `BaseBatch`

* upgrade pyright and move to a separate script

* fix linter warnings

* remove some unnecessary type casting

* remove unnecessary type casts

* use `pass` for consistency with other methods in class

* fix some more pyright warnings

* fix pyright warnings for `BaseBatch. __aenter__`

* fix `BaseTransaction. __aexit__` return type

* check length of self.columns in M2MSelect
  • Loading branch information
dantownsend authored Jun 14, 2024
1 parent 2b79717 commit e1550b4
Show file tree
Hide file tree
Showing 40 changed files with 365 additions and 190 deletions.
2 changes: 1 addition & 1 deletion piccolo/apps/fixtures/commands/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def load_json_string(
finder = Finder()
engine = engine_finder()

if not engine:
if engine is None:
raise Exception("Unable to find the engine.")

# This is what we want to the insert into the database:
Expand Down
8 changes: 8 additions & 0 deletions piccolo/apps/migrations/auto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
from .migration_manager import MigrationManager
from .schema_differ import AlterStatements, SchemaDiffer
from .schema_snapshot import SchemaSnapshot

__all__ = [
"DiffableTable",
"MigrationManager",
"AlterStatements",
"SchemaDiffer",
"SchemaSnapshot",
]
3 changes: 2 additions & 1 deletion piccolo/apps/migrations/auto/migration_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ def add_column(
cleaned_params = deserialise_params(params=params)
column = column_class(**cleaned_params)
column._meta.name = column_name
column._meta.db_column_name = db_column_name
if db_column_name:
column._meta.db_column_name = db_column_name

self.add_columns.append(
AddColumnClass(
Expand Down
4 changes: 3 additions & 1 deletion piccolo/apps/migrations/commands/backwards.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def __init__(

async def run_migrations_backwards(self, app_config: AppConfig):
migration_modules: t.Dict[str, MigrationModule] = (
self.get_migration_modules(app_config.migrations_folder_path)
self.get_migration_modules(
app_config.resolved_migrations_folder_path
)
)

ran_migration_ids = await Migration.get_migrations_which_ran(
Expand Down
2 changes: 1 addition & 1 deletion piccolo/apps/migrations/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async def get_migration_managers(
"""
migration_managers: t.List[MigrationManager] = []

migrations_folder = app_config.migrations_folder_path
migrations_folder = app_config.resolved_migrations_folder_path

migration_modules: t.Dict[str, MigrationModule] = (
self.get_migration_modules(migrations_folder)
Expand Down
2 changes: 1 addition & 1 deletion piccolo/apps/migrations/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def get_migration_statuses(self) -> t.List[MigrationStatus]:
continue

migration_modules = self.get_migration_modules(
app_config.migrations_folder_path
app_config.resolved_migrations_folder_path
)
ids = self.get_migration_ids(migration_modules)
for _id in ids:
Expand Down
2 changes: 1 addition & 1 deletion piccolo/apps/migrations/commands/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_migration_ids_to_remove(self) -> t.List[str]:
app_config = self.get_app_config(app_name=self.app_name)

migration_module_dict = self.get_migration_modules(
folder_path=app_config.migrations_folder_path
folder_path=app_config.resolved_migrations_folder_path
)

# The migration IDs which are in migration modules.
Expand Down
4 changes: 3 additions & 1 deletion piccolo/apps/migrations/commands/forwards.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ async def run_migrations(self, app_config: AppConfig) -> MigrationResult:
)

migration_modules: t.Dict[str, MigrationModule] = (
self.get_migration_modules(app_config.migrations_folder_path)
self.get_migration_modules(
app_config.resolved_migrations_folder_path
)
)

ids = self.get_migration_ids(migration_modules)
Expand Down
6 changes: 4 additions & 2 deletions piccolo/apps/migrations/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ def _generate_migration_meta(app_config: AppConfig) -> NewMigrationMeta:

filename = f"{cleaned_app_name}_{cleaned_id}"

path = os.path.join(app_config.migrations_folder_path, f"{filename}.py")
path = os.path.join(
app_config.resolved_migrations_folder_path, f"{filename}.py"
)

return NewMigrationMeta(
migration_id=_id, migration_filename=filename, migration_path=path
Expand Down Expand Up @@ -255,7 +257,7 @@ async def new(

app_config = Finder().get_app_config(app_name=app_name)

_create_migrations_folder(app_config.migrations_folder_path)
_create_migrations_folder(app_config.resolved_migrations_folder_path)

try:
await _create_new_migration(
Expand Down
4 changes: 2 additions & 2 deletions piccolo/apps/schema/commands/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def __add__(self, value: OutputSchema) -> OutputSchema:
**{"integer": BigInt, "json": JSONB},
}

COLUMN_DEFAULT_PARSER = {
COLUMN_DEFAULT_PARSER: t.Dict[t.Type[Column], t.Any] = {
BigInt: re.compile(r"^'?(?P<value>-?[0-9]\d*)'?(?:::bigint)?$"),
Boolean: re.compile(r"^(?P<value>true|false)$"),
Bytea: re.compile(r"'(?P<value>.*)'::bytea$"),
Expand Down Expand Up @@ -373,7 +373,7 @@ def __add__(self, value: OutputSchema) -> OutputSchema:
}

# Re-map for Cockroach compatibility.
COLUMN_DEFAULT_PARSER_COCKROACH = {
COLUMN_DEFAULT_PARSER_COCKROACH: t.Dict[t.Type[Column], t.Any] = {
**COLUMN_DEFAULT_PARSER,
BigInt: re.compile(r"^(?P<value>-?\d+)$"),
}
Expand Down
2 changes: 1 addition & 1 deletion piccolo/apps/shell/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def start_ipython_shell(**tables: t.Type[Table]): # pragma: no cover
if table_class_name not in existing_global_names:
globals()[table_class_name] = table_class

IPython.embed(using=_asyncio_runner, colors="neutral")
IPython.embed(using=_asyncio_runner, colors="neutral") # type: ignore


def run() -> None:
Expand Down
8 changes: 5 additions & 3 deletions piccolo/columns/column_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1956,7 +1956,9 @@ def _setup(self, table_class: t.Type[Table]) -> ForeignKeySetupResponse:

if is_table_class:
# Record the reverse relationship on the target table.
references._meta._foreign_key_references.append(self)
t.cast(
t.Type[Table], references
)._meta._foreign_key_references.append(self)

# Allow columns on the referenced table to be accessed via
# auto completion.
Expand Down Expand Up @@ -2710,7 +2712,7 @@ def all(self, value: t.Any) -> Where:
else:
raise ValueError("Unrecognised engine type")

def cat(self, value: t.List[t.Any]) -> QueryString:
def cat(self, value: t.Union[t.Any, t.List[t.Any]]) -> QueryString:
"""
Used in an ``update`` query to append items to an array.
Expand Down Expand Up @@ -2741,7 +2743,7 @@ def cat(self, value: t.List[t.Any]) -> QueryString:
db_column_name = self._meta.db_column_name
return QueryString(f'array_cat("{db_column_name}", {{}})', value)

def __add__(self, value: t.List[t.Any]) -> QueryString:
def __add__(self, value: t.Union[t.Any, t.List[t.Any]]) -> QueryString:
return self.cat(value)

###########################################################################
Expand Down
2 changes: 1 addition & 1 deletion piccolo/columns/defaults/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def sqlite(self) -> str:
pass

@abstractmethod
def python(self):
def python(self) -> t.Any:
pass

def get_postgres_interval_string(self, attributes: t.List[str]) -> str:
Expand Down
10 changes: 9 additions & 1 deletion piccolo/columns/defaults/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,15 @@ def from_date(cls, instance: datetime.date):


# Might add an enum back which encapsulates all of the options.
DateArg = t.Union[DateOffset, DateCustom, DateNow, Enum, None, datetime.date]
DateArg = t.Union[
DateOffset,
DateCustom,
DateNow,
Enum,
None,
datetime.date,
t.Callable[[], datetime.date],
]


__all__ = ["DateArg", "DateOffset", "DateCustom", "DateNow"]
1 change: 1 addition & 0 deletions piccolo/columns/defaults/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def from_timedelta(cls, instance: datetime.timedelta):
Enum,
None,
datetime.timedelta,
t.Callable[[], datetime.timedelta],
]


Expand Down
10 changes: 9 additions & 1 deletion piccolo/columns/defaults/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ def from_time(cls, instance: datetime.time):
)


TimeArg = t.Union[TimeCustom, TimeNow, TimeOffset, Enum, None, datetime.time]
TimeArg = t.Union[
TimeCustom,
TimeNow,
TimeOffset,
Enum,
None,
datetime.time,
t.Callable[[], datetime.time],
]


__all__ = ["TimeArg", "TimeCustom", "TimeNow", "TimeOffset"]
1 change: 1 addition & 0 deletions piccolo/columns/defaults/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class DatetimeDefault:
None,
datetime.datetime,
DatetimeDefault,
t.Callable[[], datetime.datetime],
]


Expand Down
2 changes: 1 addition & 1 deletion piccolo/columns/defaults/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def python(self):
return uuid.uuid4()


UUIDArg = t.Union[UUID4, uuid.UUID, str, Enum, None]
UUIDArg = t.Union[UUID4, uuid.UUID, str, Enum, None, t.Callable[[], uuid.UUID]]


__all__ = ["UUIDArg", "UUID4"]
14 changes: 7 additions & 7 deletions piccolo/columns/m2m.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def get_select_string(
if len(self.columns) > 1 or not self.serialisation_safe:
column_name = table_2_pk_name
else:
assert len(self.columns) > 0
column_name = self.columns[0]._meta.db_column_name

return QueryString(
Expand Down Expand Up @@ -256,15 +257,14 @@ def secondary_table(self) -> t.Type[Table]:

@dataclass
class M2MAddRelated:

target_row: Table
m2m: M2M
rows: t.Sequence[Table]
extra_column_values: t.Dict[t.Union[Column, str], t.Any]

def __post_init__(self) -> None:
# Normalise `extra_column_values`, so we just have the column names.
self.extra_column_values: t.Dict[str, t.Any] = {
@property
def resolved_extra_column_values(self) -> t.Dict[str, t.Any]:
return {
i._meta.name if isinstance(i, Column) else i: j
for i, j in self.extra_column_values.items()
}
Expand All @@ -281,7 +281,9 @@ async def _run(self):
joining_table_rows = []

for row in rows:
joining_table_row = joining_table(**self.extra_column_values)
joining_table_row = joining_table(
**self.resolved_extra_column_values
)
setattr(
joining_table_row,
self.m2m._meta.primary_foreign_key._meta.name,
Expand Down Expand Up @@ -323,7 +325,6 @@ def __await__(self):

@dataclass
class M2MRemoveRelated:

target_row: Table
m2m: M2M
rows: t.Sequence[Table]
Expand Down Expand Up @@ -363,7 +364,6 @@ def __await__(self):

@dataclass
class M2MGetRelated:

row: Table
m2m: M2M

Expand Down
13 changes: 9 additions & 4 deletions piccolo/conf/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,22 @@ class AppConfig:
"""

app_name: str
migrations_folder_path: str
migrations_folder_path: t.Union[str, pathlib.Path]
table_classes: t.List[t.Type[Table]] = field(default_factory=list)
migration_dependencies: t.List[str] = field(default_factory=list)
commands: t.List[t.Union[t.Callable, Command]] = field(
default_factory=list
)

def __post_init__(self) -> None:
if isinstance(self.migrations_folder_path, pathlib.Path):
self.migrations_folder_path = str(self.migrations_folder_path)
@property
def resolved_migrations_folder_path(self) -> str:
return (
str(self.migrations_folder_path)
if isinstance(self.migrations_folder_path, pathlib.Path)
else self.migrations_folder_path
)

def __post_init__(self) -> None:
self._migration_dependency_app_configs: t.Optional[
t.List[AppConfig]
] = None
Expand Down
Loading

0 comments on commit e1550b4

Please sign in to comment.