Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change type hints to be compatible with Python 3.9 #46

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "flask-moreshell"
version = "0.1.2"
version = "0.1.3"
description = "Flask Moreshell"
authors = ["JAEGYUN JUNG <[email protected]>"]
license = "MIT"
Expand Down
50 changes: 23 additions & 27 deletions src/flask_moreshell/__main__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import importlib
import sys

import click
from flask.cli import with_appcontext

from flask_moreshell.shells import BPythonShell
from flask_moreshell.shells import IpythonShell
from flask_moreshell.shells import PTPythonShell
from flask_moreshell.shells import PythonShell


shells = {
"ipython": IpythonShell,
"bpython": BPythonShell,
"ptpython": PTPythonShell,
"python": PythonShell,
}


@click.command(context_settings=dict(ignore_unknown_options=True))
@click.option(
Expand All @@ -35,23 +23,31 @@ def shell(shelltype: str) -> None:

:param shelltype: type of shell to use.
"""
shell_classes = {
"ipython": "IPythonShell",
"bpython": "BPythonShell",
"ptpython": "PTPythonShell",
"python": "PythonShell",
}

def try_load_shell(_shelltype: str) -> None:
shell_class = shells.get(_shelltype)
assert shell_class is not None # noqa S101
shell_class().load() # type: ignore

# If the user specifies a shell type, try to load it
if shelltype:
try_load_shell(shelltype)
shell_class = shell_classes[shelltype]
try:
shell_module = importlib.import_module(
f"flask_moreshell.shells.{shelltype}_shell"
)
shell_class = getattr(shell_module, shell_class)
shell_class().load()
except ModuleNotFoundError as e:
print(e)
else:
preferred_shells = ["ipython", "bpython", "ptpython", "python"]
for shelltype in preferred_shells:
for shell_class in shell_classes.keys():
try:
try_load_shell(shelltype)
break
shell_module = importlib.import_module(
f"flask_moreshell.shells.{shell_class}_shell"
)
shell_class = getattr(shell_module, shell_classes[shell_class])
shell_class().load()
sys.exit(0)
except ModuleNotFoundError:
continue
if not shelltype:
print("No shell type is installed or recognized on your system.")
sys.exit(1)
14 changes: 0 additions & 14 deletions src/flask_moreshell/shells/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +0,0 @@
from .base_shell import BaseShell
from .bpython_shell import BPythonShell
from .ipython_shell import IpythonShell
from .ptpython_shell import PTPythonShell
from .python_shell import PythonShell


__all__ = [
"BaseShell",
"BPythonShell",
"IpythonShell",
"PTPythonShell",
"PythonShell",
]
2 changes: 1 addition & 1 deletion src/flask_moreshell/shells/bpython_shell.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask_moreshell.shells import BaseShell
from flask_moreshell.shells.base_shell import BaseShell


try:
Expand Down
8 changes: 5 additions & 3 deletions src/flask_moreshell/shells/ipython_shell.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Any
from typing import Optional
from typing import Union

from flask.globals import current_app

from flask_moreshell.shells import BaseShell
from flask_moreshell.shells.base_shell import BaseShell


try:
Expand All @@ -13,7 +15,7 @@
raise ModuleNotFoundError("IPython is not installed on your system.") from e


class IpythonShell(BaseShell):
class IPythonShell(BaseShell):
def get_shell_name(self) -> str:
return "IPython"

Expand All @@ -31,7 +33,7 @@ def load(self) -> None:
)

@staticmethod
def _get_config() -> Any | None:
def _get_config() -> Union[Config, Optional[Any]]:
if "IPYTHON_CONFIG" in current_app.config:
return Config(current_app.config["IPYTHON_CONFIG"])
return load_default_config() # type: ignore
8 changes: 6 additions & 2 deletions src/flask_moreshell/shells/ptpython_shell.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import sys
from importlib.metadata import version

from ptpython.repl import embed

from flask_moreshell.shells import BaseShell
try:
from ptpython.repl import embed
except ModuleNotFoundError as e:
raise ModuleNotFoundError("PTPython is not installed on your system.") from e

from flask_moreshell.shells.base_shell import BaseShell


class PTPythonShell(BaseShell):
Expand Down
2 changes: 1 addition & 1 deletion src/flask_moreshell/shells/python_shell.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import code
import sys

from flask_moreshell.shells import BaseShell
from flask_moreshell.shells.base_shell import BaseShell


class PythonShell(BaseShell):
Expand Down
2 changes: 1 addition & 1 deletion tests/shells/test_bpython_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from flask_moreshell.shells import BPythonShell
from flask_moreshell.shells.bpython_shell import BPythonShell


@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions tests/shells/test_ipython_shell.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest

from flask_moreshell.shells import IpythonShell
from flask_moreshell.shells.ipython_shell import IPythonShell


@pytest.fixture
def ipython_shell():
return IpythonShell()
return IPythonShell()


def test_get_shell_name(ipython_shell):
Expand Down
2 changes: 1 addition & 1 deletion tests/shells/test_ptpython_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from flask import Flask

from flask_moreshell.shells import PTPythonShell
from flask_moreshell.shells.ptpython_shell import PTPythonShell


@pytest.fixture
Expand Down