Skip to content

Commit

Permalink
Merge 866211d into 7c4bcb9
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Nov 15, 2020
2 parents 7c4bcb9 + 866211d commit d7338af
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 45 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ before_script:
- psql -c "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";" -d piccolo -U postgres
script:
- flake8 piccolo
- mypy piccolo
- cd tests && ./travis.sh
after_success:
- cd .. && coveralls
2 changes: 1 addition & 1 deletion piccolo/apps/app/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
import typing as t

import black
import black # type: ignore
import jinja2


Expand Down
4 changes: 2 additions & 2 deletions piccolo/apps/asgi/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import shutil
import typing as t

import black
import colorama
import black # type: ignore
import colorama # type: ignore
from jinja2 import Environment, FileSystemLoader


Expand Down
2 changes: 1 addition & 1 deletion piccolo/apps/migrations/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import typing as t
from types import ModuleType

import black
import black # type: ignore
import jinja2

from .base import BaseMigrationManager
Expand Down
4 changes: 2 additions & 2 deletions piccolo/apps/playground/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def run(
Postgres port
"""
try:
import IPython
import IPython # type: ignore
except ImportError:
print(
"Install iPython using `pip install ipython` to use this feature."
Expand Down Expand Up @@ -176,6 +176,6 @@ def run(

populate()

from IPython.core.interactiveshell import _asyncio_runner
from IPython.core.interactiveshell import _asyncio_runner # type: ignore

IPython.embed(using=_asyncio_runner)
2 changes: 1 addition & 1 deletion piccolo/apps/project/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import sys

import black
import black # type: ignore
import jinja2


Expand Down
4 changes: 2 additions & 2 deletions piccolo/apps/shell/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def start_ipython_shell(**tables: t.Dict[str, t.Type[Table]]):
try:
import IPython
import IPython # type: ignore
except ImportError:
print(
"Install iPython using `pip install ipython` to use this feature."
Expand All @@ -19,7 +19,7 @@ def start_ipython_shell(**tables: t.Dict[str, t.Type[Table]]):
if table_class_name not in existing_global_names:
globals()[table_class_name] = table_class

from IPython.core.interactiveshell import _asyncio_runner
from IPython.core.interactiveshell import _asyncio_runner # type: ignore

IPython.embed(using=_asyncio_runner)

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

@classmethod
def from_datetime(cls, instance: datetime.datetime):
def from_datetime(cls, instance: datetime.datetime): # type: ignore
return cls(
year=instance.year,
month=instance.month,
Expand Down
22 changes: 14 additions & 8 deletions piccolo/conf/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ def get_table_classes(self, app_name: str) -> t.List[t.Type[Table]]:
"""
Returns each Table subclass defined in the given app if it exists.
Otherwise raises a ValueError.
:raises ValueError:
If an AppConfig can't be found for the given app_name.
"""
app_config = self.get_app_config(app_name=app_name)
if not app_config:
Expand All @@ -186,9 +190,12 @@ def get_table_with_name(
Otherwise raises a ValueError.
"""
app_config = self.get_app_config(app_name=app_name)
return app_config.get_table_with_name(
table_class_name=table_class_name
)
if app_config is None:
raise ValueError(f"Can't find an app_config for {app_name}")
else:
return app_config.get_table_with_name(
table_class_name=table_class_name
)


class PiccoloConfModule(ModuleType):
Expand Down Expand Up @@ -270,10 +277,8 @@ def get_piccolo_conf_module(
if not module_name:
module_name = DEFAULT_MODULE_NAME

module: t.Optional[PiccoloConfModule] = None

try:
module = import_module(module_name)
module = t.cast(PiccoloConfModule, import_module(module_name))
except ModuleNotFoundError:
if self.diagnose:
colored_warning(
Expand All @@ -284,8 +289,9 @@ def get_piccolo_conf_module(
level=Level.high,
)
print(traceback.format_exc())

return module
return None
else:
return module

def get_app_registry(self) -> AppRegistry:
"""
Expand Down
10 changes: 5 additions & 5 deletions piccolo/engine/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import typing as t
import warnings

import asyncpg
from asyncpg.connection import Connection
from asyncpg.cursor import Cursor
from asyncpg.exceptions import InsufficientPrivilegeError
from asyncpg.pool import Pool
import asyncpg # type: ignore
from asyncpg.connection import Connection # type: ignore
from asyncpg.cursor import Cursor # type: ignore
from asyncpg.exceptions import InsufficientPrivilegeError # type: ignore
from asyncpg.pool import Pool # type: ignore

from piccolo.engine.base import Batch, Engine
from piccolo.engine.exceptions import TransactionError
Expand Down
6 changes: 3 additions & 3 deletions piccolo/engine/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ async def __aexit__(self, exception_type, exception, traceback):
###############################################################################


def dict_factory(cursor, row):
def dict_factory(cursor, row) -> t.Dict:
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
Expand Down Expand Up @@ -354,7 +354,7 @@ async def batch(self, query: Query, batch_size: int = 100) -> AsyncBatch:

async def get_connection(self) -> Connection:
connection = await aiosqlite.connect(**self.connection_kwargs)
connection.row_factory = dict_factory
connection.row_factory = dict_factory # type: ignore
await connection.execute("PRAGMA foreign_keys = 1")
return connection

Expand All @@ -366,7 +366,7 @@ async def _run_in_new_connection(
async with aiosqlite.connect(**self.connection_kwargs) as connection:
await connection.execute("PRAGMA foreign_keys = 1")

connection.row_factory = dict_factory
connection.row_factory = dict_factory # type: ignore
async with connection.execute(query, args) as cursor:
await connection.commit()
response = await cursor.fetchall()
Expand Down
2 changes: 1 addition & 1 deletion piccolo/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys

from targ import CLI
from targ import CLI # type: ignore

from piccolo.conf.apps import AppRegistry, Finder
from piccolo.apps.app.piccolo_app import APP_CONFIG as app_config
Expand Down
2 changes: 1 addition & 1 deletion piccolo/query/methods/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def querystrings(self) -> t.Sequence[QueryString]:
query = f"{base} ({columns_sql})"
create_table = QueryString(query, *[i.querystring for i in columns])

create_indexes = []
create_indexes: t.List[QueryString] = []
for column in columns:
if column._meta.index:
create_indexes.extend(
Expand Down
26 changes: 19 additions & 7 deletions piccolo/query/methods/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Select(Query):
def __init__(
self,
table: t.Type[Table],
columns_list: t.Sequence[Selectable] = [],
columns_list: t.Sequence[t.Union[Selectable, str]] = [],
exclude_secrets: bool = False,
):
super().__init__(table)
Expand All @@ -80,7 +80,7 @@ def __init__(

self.columns(*columns_list)

def columns(self, *columns: t.Union[Column, str]) -> Select:
def columns(self, *columns: t.Union[Selectable, str]) -> Select:
_columns = self.table._process_column_args(*columns)
self.columns_delegate.columns(*_columns)
return self
Expand All @@ -90,8 +90,12 @@ def distinct(self) -> Select:
return self

def group_by(self, *columns: Column) -> Select:
columns = self.table._process_column_args(*columns)
self.group_by_delegate.group_by(*columns)
_columns: t.List[Column] = [
i
for i in self.table._process_column_args(*columns)
if isinstance(i, Column)
]
self.group_by_delegate.group_by(*_columns)
return self

def limit(self, number: int) -> Select:
Expand All @@ -116,8 +120,12 @@ async def response_handler(self, response):
return response

def order_by(self, *columns: Column, ascending=True) -> Select:
columns = self.table._process_column_args(*columns)
self.order_by_delegate.order_by(*columns, ascending=ascending)
_columns: t.List[Column] = [
i
for i in self.table._process_column_args(*columns)
if isinstance(i, Column)
]
self.order_by_delegate.order_by(*_columns, ascending=ascending)
return self

def output(self, **kwargs) -> Select:
Expand All @@ -144,7 +152,11 @@ def _get_joins(self, columns: t.Sequence[Selectable]) -> t.List[str]:
"""
joins: t.List[str] = []

readables = [i for i in columns if isinstance(i, Readable)]
readables: t.List[Readable] = [
i for i in columns if isinstance(i, Readable)
]

columns = list(columns)
for readable in readables:
columns += readable.columns

Expand Down
8 changes: 5 additions & 3 deletions piccolo/query/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

if t.TYPE_CHECKING:
from piccolo.table import Table # noqa
from piccolo.columns.base import Selectable


@dataclass
Expand Down Expand Up @@ -190,10 +191,11 @@ class ColumnsDelegate:
.columns(MyTable.column_a, MyTable.column_b)
"""

selected_columns: t.List[Column] = field(default_factory=list)
selected_columns: t.Sequence[Selectable] = field(default_factory=list)

def columns(self, *columns: Column):
self.selected_columns += columns
def columns(self, *columns: Selectable):
combined = list(self.selected_columns) + list(columns)
self.selected_columns = combined

def remove_secret_columns(self):
self.selected_columns = [
Expand Down
12 changes: 7 additions & 5 deletions piccolo/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ def get_related(self, foreign_key: t.Union[ForeignKey, str]) -> Objects:
"""
if isinstance(foreign_key, str):
foreign_key = self._meta.get_column_by_name(foreign_key)
column = self._meta.get_column_by_name(foreign_key)
if isinstance(column, ForeignKey):
foreign_key = column

if not isinstance(foreign_key, ForeignKey):
raise ValueError(
Expand Down Expand Up @@ -448,9 +450,9 @@ def select(
the response. Even though passwords are hashed, you still don't want
them being passed over the network if avoidable.
"""
columns = cls._process_column_args(*columns)
_columns = cls._process_column_args(*columns)
return Select(
table=cls, columns_list=columns, exclude_secrets=exclude_secrets
table=cls, columns_list=_columns, exclude_secrets=exclude_secrets
)

@classmethod
Expand Down Expand Up @@ -560,7 +562,7 @@ def indexes(cls) -> Indexes:
@classmethod
def create_index(
cls,
columns: t.Sequence[Column, str],
columns: t.List[t.Union[Column, str]],
method: IndexMethod = IndexMethod.btree,
) -> CreateIndex:
"""
Expand All @@ -573,7 +575,7 @@ def create_index(

@classmethod
def drop_index(
cls, columns: t.Sequence[Column, str], if_exists: bool = True
cls, columns: t.List[t.Union[Column, str]], if_exists: bool = True
) -> DropIndex:
"""
Drop a table index. If multiple columns are specified, this refers
Expand Down
2 changes: 1 addition & 1 deletion piccolo/utils/warnings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum
import warnings

import colorama
import colorama # type: ignore


colorama.init()
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
aiosqlite==0.16.0
asyncpg==0.21.0
black==19.10b0
black==20.8b1
colorama==0.4.*
Jinja2==2.11.2
targ==0.1.*

0 comments on commit d7338af

Please sign in to comment.