Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b14df38

Browse files
authored
Simplify color definition. (#14291)
Schemes either accept a dict, or as kwargs. Try to use a single way of defining via a dict, so that we can later deprecate passing all as kwargs. Use the opportunity to fix some things linter complaints, and add type annotations.
2 parents 8889ec7 + 6b2687c commit b14df38

7 files changed

Lines changed: 149 additions & 127 deletions

File tree

IPython/core/alias.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,28 @@ def __call__(self, rest=''):
190190
#-----------------------------------------------------------------------------
191191

192192
class AliasManager(Configurable):
193-
194-
default_aliases = List(default_aliases()).tag(config=True)
195-
user_aliases = List(default_value=[]).tag(config=True)
196-
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
193+
default_aliases: List = List(default_aliases()).tag(config=True)
194+
user_aliases: List = List(default_value=[]).tag(config=True)
195+
shell = Instance(
196+
"IPython.core.interactiveshell.InteractiveShellABC", allow_none=True
197+
)
197198

198199
def __init__(self, shell=None, **kwargs):
199200
super(AliasManager, self).__init__(shell=shell, **kwargs)
200201
# For convenient access
201-
self.linemagics = self.shell.magics_manager.magics['line']
202-
self.init_aliases()
202+
if self.shell is not None:
203+
self.linemagics = self.shell.magics_manager.magics["line"]
204+
self.init_aliases()
203205

204206
def init_aliases(self):
205207
# Load default & user aliases
206208
for name, cmd in self.default_aliases + self.user_aliases:
207-
if cmd.startswith('ls ') and self.shell.colors == 'NoColor':
208-
cmd = cmd.replace(' --color', '')
209+
if (
210+
cmd.startswith("ls ")
211+
and self.shell is not None
212+
and self.shell.colors == "NoColor"
213+
):
214+
cmd = cmd.replace(" --color", "")
209215
self.soft_define_alias(name, cmd)
210216

211217
@property
@@ -246,7 +252,7 @@ def undefine_alias(self, name):
246252
raise ValueError('%s is not an alias' % name)
247253

248254
def clear_aliases(self):
249-
for name, cmd in self.aliases:
255+
for name, _ in self.aliases:
250256
self.undefine_alias(name)
251257

252258
def retrieve_alias(self, name):

IPython/core/excolors.py

Lines changed: 118 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -42,118 +42,128 @@ def exception_colors():
4242
ex_colors = ColorSchemeTable()
4343

4444
# Populate it with color schemes
45-
C = TermColors # shorthand and local lookup
46-
ex_colors.add_scheme(ColorScheme(
47-
'NoColor',
48-
# The color to be used for the top line
49-
topline = C.NoColor,
50-
51-
# The colors to be used in the traceback
52-
filename = C.NoColor,
53-
lineno = C.NoColor,
54-
name = C.NoColor,
55-
vName = C.NoColor,
56-
val = C.NoColor,
57-
em = C.NoColor,
58-
59-
# Emphasized colors for the last frame of the traceback
60-
normalEm = C.NoColor,
61-
filenameEm = C.NoColor,
62-
linenoEm = C.NoColor,
63-
nameEm = C.NoColor,
64-
valEm = C.NoColor,
65-
66-
# Colors for printing the exception
67-
excName = C.NoColor,
68-
line = C.NoColor,
69-
caret = C.NoColor,
70-
Normal = C.NoColor
71-
))
45+
C = TermColors # shorthand and local lookup
46+
ex_colors.add_scheme(
47+
ColorScheme(
48+
"NoColor",
49+
{
50+
# The color to be used for the top line
51+
"topline": C.NoColor,
52+
53+
# The colors to be used in the traceback
54+
"filename": C.NoColor,
55+
"lineno": C.NoColor,
56+
"name": C.NoColor,
57+
"vName": C.NoColor,
58+
"val": C.NoColor,
59+
"em": C.NoColor,
60+
61+
# Emphasized colors for the last frame of the traceback
62+
"normalEm": C.NoColor,
63+
"filenameEm": C.NoColor,
64+
"linenoEm": C.NoColor,
65+
"nameEm": C.NoColor,
66+
"valEm": C.NoColor,
67+
68+
# Colors for printing the exception
69+
"excName": C.NoColor,
70+
"line": C.NoColor,
71+
"caret": C.NoColor,
72+
"Normal": C.NoColor,
73+
},
74+
)
75+
)
7276

7377
# make some schemes as instances so we can copy them for modification easily
74-
ex_colors.add_scheme(ColorScheme(
75-
'Linux',
76-
# The color to be used for the top line
77-
topline = C.LightRed,
78-
79-
# The colors to be used in the traceback
80-
filename = C.Green,
81-
lineno = C.Green,
82-
name = C.Purple,
83-
vName = C.Cyan,
84-
val = C.Green,
85-
em = C.LightCyan,
86-
87-
# Emphasized colors for the last frame of the traceback
88-
normalEm = C.LightCyan,
89-
filenameEm = C.LightGreen,
90-
linenoEm = C.LightGreen,
91-
nameEm = C.LightPurple,
92-
valEm = C.LightBlue,
93-
94-
# Colors for printing the exception
95-
excName = C.LightRed,
96-
line = C.Yellow,
97-
caret = C.White,
98-
Normal = C.Normal
99-
))
78+
ex_colors.add_scheme(
79+
ColorScheme(
80+
"Linux",
81+
{
82+
# The color to be used for the top line
83+
"topline": C.LightRed,
84+
# The colors to be used in the traceback
85+
"filename": C.Green,
86+
"lineno": C.Green,
87+
"name": C.Purple,
88+
"vName": C.Cyan,
89+
"val": C.Green,
90+
"em": C.LightCyan,
91+
# Emphasized colors for the last frame of the traceback
92+
"normalEm": C.LightCyan,
93+
"filenameEm": C.LightGreen,
94+
"linenoEm": C.LightGreen,
95+
"nameEm": C.LightPurple,
96+
"valEm": C.LightBlue,
97+
# Colors for printing the exception
98+
"excName": C.LightRed,
99+
"line": C.Yellow,
100+
"caret": C.White,
101+
"Normal": C.Normal,
102+
},
103+
)
104+
)
100105

101106
# For light backgrounds, swap dark/light colors
102-
ex_colors.add_scheme(ColorScheme(
103-
'LightBG',
104-
# The color to be used for the top line
105-
topline = C.Red,
106-
107-
# The colors to be used in the traceback
108-
filename = C.LightGreen,
109-
lineno = C.LightGreen,
110-
name = C.LightPurple,
111-
vName = C.Cyan,
112-
val = C.LightGreen,
113-
em = C.Cyan,
114-
115-
# Emphasized colors for the last frame of the traceback
116-
normalEm = C.Cyan,
117-
filenameEm = C.Green,
118-
linenoEm = C.Green,
119-
nameEm = C.Purple,
120-
valEm = C.Blue,
121-
122-
# Colors for printing the exception
123-
excName = C.Red,
124-
#line = C.Brown, # brown often is displayed as yellow
125-
line = C.Red,
126-
caret = C.Normal,
127-
Normal = C.Normal,
128-
))
129-
130-
ex_colors.add_scheme(ColorScheme(
131-
'Neutral',
132-
# The color to be used for the top line
133-
topline = C.Red,
134-
135-
# The colors to be used in the traceback
136-
filename = C.LightGreen,
137-
lineno = C.LightGreen,
138-
name = C.LightPurple,
139-
vName = C.Cyan,
140-
val = C.LightGreen,
141-
em = C.Cyan,
142-
143-
# Emphasized colors for the last frame of the traceback
144-
normalEm = C.Cyan,
145-
filenameEm = C.Green,
146-
linenoEm = C.Green,
147-
nameEm = C.Purple,
148-
valEm = C.Blue,
149-
150-
# Colors for printing the exception
151-
excName = C.Red,
152-
#line = C.Brown, # brown often is displayed as yellow
153-
line = C.Red,
154-
caret = C.Normal,
155-
Normal = C.Normal,
156-
))
107+
ex_colors.add_scheme(
108+
ColorScheme(
109+
"LightBG",
110+
{
111+
# The color to be used for the top line
112+
"topline": C.Red,
113+
114+
# The colors to be used in the traceback
115+
"filename": C.LightGreen,
116+
"lineno": C.LightGreen,
117+
"name": C.LightPurple,
118+
"vName": C.Cyan,
119+
"val": C.LightGreen,
120+
"em": C.Cyan,
121+
122+
# Emphasized colors for the last frame of the traceback
123+
"normalEm": C.Cyan,
124+
"filenameEm": C.Green,
125+
"linenoEm": C.Green,
126+
"nameEm": C.Purple,
127+
"valEm": C.Blue,
128+
129+
# Colors for printing the exception
130+
"excName": C.Red,
131+
# "line": C.Brown, # brown often is displayed as yellow
132+
"line": C.Red,
133+
"caret": C.Normal,
134+
"Normal": C.Normal,
135+
},
136+
)
137+
)
138+
139+
ex_colors.add_scheme(
140+
ColorScheme(
141+
"Neutral",
142+
{
143+
# The color to be used for the top line
144+
"topline": C.Red,
145+
# The colors to be used in the traceback
146+
"filename": C.LightGreen,
147+
"lineno": C.LightGreen,
148+
"name": C.LightPurple,
149+
"vName": C.Cyan,
150+
"val": C.LightGreen,
151+
"em": C.Cyan,
152+
# Emphasized colors for the last frame of the traceback
153+
"normalEm": C.Cyan,
154+
"filenameEm": C.Green,
155+
"linenoEm": C.Green,
156+
"nameEm": C.Purple,
157+
"valEm": C.Blue,
158+
# Colors for printing the exception
159+
"excName": C.Red,
160+
# line = C.Brown, # brown often is displayed as yellow
161+
"line": C.Red,
162+
"caret": C.Normal,
163+
"Normal": C.Normal,
164+
},
165+
)
166+
)
157167

158168
# Hack: the 'neutral' colours are not very visible on a dark background on
159169
# Windows. Since Windows command prompts have a dark background by default, and

IPython/core/extensions.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
from importlib import import_module, reload
1111

1212
from traitlets.config.configurable import Configurable
13-
from IPython.utils.path import ensure_dir_exists, compress_user
14-
from IPython.utils.decorators import undoc
13+
from IPython.utils.path import ensure_dir_exists
1514
from traitlets import Instance
1615

1716

@@ -84,7 +83,7 @@ def _load_extension(self, module_str: str):
8483
if module_str in self.loaded:
8584
return "already loaded"
8685

87-
from IPython.utils.syspathcontext import prepended_to_syspath
86+
assert self.shell is not None
8887

8988
with self.shell.builtin_trap:
9089
if module_str not in sys.modules:

IPython/core/payload.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
#-----------------------------------------------------------------------------
2727

2828
class PayloadManager(Configurable):
29-
30-
_payload = List([])
29+
_payload: List = List([])
3130

3231
def write_payload(self, data, single=True):
3332
"""Include or update the specified `data` payload in the PayloadManager.

IPython/terminal/prompts.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ def write_output_prompt(self):
103103

104104
if self.do_full_cache:
105105
tokens = self.shell.prompts.out_prompt_tokens()
106-
prompt_txt = ''.join(s for t, s in tokens)
107-
if prompt_txt and not prompt_txt.endswith('\n'):
106+
prompt_txt = "".join(s for _, s in tokens)
107+
if prompt_txt and not prompt_txt.endswith("\n"):
108108
# Ask for a newline before multiline output
109109
self.prompt_end_newline = False
110110

@@ -116,6 +116,7 @@ def write_output_prompt(self):
116116
sys.stdout.write(prompt_txt)
117117

118118
def write_format_data(self, format_dict, md_dict=None) -> None:
119+
assert self.shell is not None
119120
if self.shell.mime_renderers:
120121

121122
for mime, handler in self.shell.mime_renderers.items():

IPython/utils/coloransi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""Tools for coloring text in ANSI terminals.
32
"""
43

@@ -9,12 +8,13 @@
98
# the file COPYING, distributed as part of this software.
109
#*****************************************************************************
1110

12-
__all__ = ['TermColors','InputTermColors','ColorScheme','ColorSchemeTable']
1311

1412
import os
1513

1614
from IPython.utils.ipstruct import Struct
1715

16+
__all__ = ["TermColors", "InputTermColors", "ColorScheme", "ColorSchemeTable"]
17+
1818
color_templates = (
1919
# Dark colors
2020
("Black" , "0;30"),
@@ -110,6 +110,10 @@ class NoColors:
110110

111111
class ColorScheme:
112112
"""Generic color scheme class. Just a name and a Struct."""
113+
114+
name: str
115+
colors: Struct
116+
113117
def __init__(self,__scheme_name_,colordict=None,**colormap):
114118
self.name = __scheme_name_
115119
if colordict is None:

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["setuptools >= 51.0.0"]
33
build-backend = "setuptools.build_meta"
44

55
[tool.mypy]
6-
python_version = 3.10
6+
python_version = "3.10"
77
ignore_missing_imports = true
88
follow_imports = 'silent'
99
exclude = [
@@ -79,3 +79,6 @@ ipdoctest_optionflags = [
7979
"ELLIPSIS"
8080
]
8181
asyncio_mode = "strict"
82+
83+
[tool.pyright]
84+
pythonPlatform="All"

0 commit comments

Comments
 (0)