Skip to content

Commit a1fb91a

Browse files
committed
MSS: improve type annotations and add CI check
1 parent 0494d26 commit a1fb91a

23 files changed

+205
-139
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ MANIFEST*
1414
.tox
1515
.vscode
1616
docs/output/
17+
.mypy_cache/

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ language: python
22
dist: xenial
33

44
matrix:
5+
fast_finish: true
56
include:
67
- os: linux
78
python: "3.7"
89
env: TOXENV=lint
10+
- os: linux
11+
python: "3.7"
12+
env: TOXENV=types
13+
- os: linux
14+
python: "3.7"
15+
env: TOXENV=docs
916
- os: osx
1017
language: generic
1118
env:

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ History:
22

33
<see Git checking messages for history>
44

5+
5.0.0 2019/xx/xx
6+
- removed support for Python 2.7
7+
- MSS: improve type annotations and add CI check
8+
59
4.0.1 2019/01/26
610
- Linux: fix several XLib functions signature (fixes #92)
711
- Linux: improve monitors finding by a factor of 44

CHANGES.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
5.0.0 (2019-xx-xx)
2+
==================
3+
4+
linux.py
5+
--------
6+
- Deleted ``LAST_ERROR`` constant. Use ``ERROR`` namespace instead, specially the ``ERROR.details`` attribute.
7+
8+
models.py
9+
---------
10+
- Added ``Monitor``
11+
- Added ``Monitors``
12+
- Added ``Pixel``
13+
- Added ``Pixels``
14+
- Added ``Pos``
15+
- Added ``Size``
16+
17+
screenshot.py
18+
-------------
19+
- Removed ``Pos``. Use ``models.Pos`` instead.
20+
- Removed ``Size``. Use ``models.Size`` instead.
21+
22+
123
4.0.1 (2019-01-26)
224
==================
325

docs/source/api.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ GNU/Linux
1010

1111
.. module:: mss.linux
1212

13-
.. attribute:: LAST_ERROR
13+
.. attribute:: ERROR
1414

15-
:type: dict[str, Any]
15+
:type: types.SimpleNamspacedict
1616

17-
Contains the latest Xlib or XRANDR function.
17+
The `details` attribute contains the latest Xlib or XRANDR function. It is a dict.
1818

19-
.. versionadded:: 4.0.0
19+
.. versionadded:: 5.0.0
2020

2121
.. class:: MSS
2222

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# built documents.
3030
#
3131
# The short X.Y version.
32-
version = "4.0.1"
32+
version = "5.0.0"
3333

3434
# The full version, including alpha/beta/rc tags.
3535
release = "latest"

docs/source/developers.rst

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,20 @@ Launch the test suit::
4141
This will test MSS and ensure a good code quality.
4242

4343

44-
Static Type Checking
45-
====================
46-
47-
`mypy <http://mypy-lang.org/>`_ is a compile-time static type checker for Python, allowing optional, gradual typing of Python code.
48-
MSS is using special files syntax for type annotations, which means that type annotations are written inside **pyi** files.
44+
Code Quality
45+
============
4946

47+
To ensure the code is always well enough using `flake8 <https://pypi.org/project/flake8/>`_::
5048

51-
Dependencies
52-
------------
53-
54-
Install required packages::
49+
$ TOXENV=lint tox
5550

56-
$ python -m pip install --upgrade --user mypy-lang
5751

52+
Static Type Checking
53+
====================
5854

59-
Running Mypy
60-
------------
61-
62-
::
55+
To check type annotation using `mypy <http://mypy-lang.org/>`_::
6356

64-
$ sh check-types.sh -p mss
57+
$ TOXENV=types tox
6558

6659

6760
Documentation

mss/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .exception import ScreenShotError
1414
from .factory import mss
1515

16-
__version__ = "4.0.1"
16+
__version__ = "5.0.0"
1717
__author__ = "Mickaël 'Tiger-222' Schoentgen"
1818
__copyright__ = """
1919
Copyright (c) 2013-2019, Mickaël 'Tiger-222' Schoentgen

mss/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
import os.path
1010
import sys
1111
from argparse import ArgumentParser
12+
from typing import TYPE_CHECKING
1213

1314
from . import __version__
1415
from .exception import ScreenShotError
1516
from .factory import mss
1617
from .tools import to_png
1718

19+
if TYPE_CHECKING:
20+
from typing import List, Optional # noqa
21+
1822

1923
def main(args=None):
2024
# type: (Optional[List[str]]) -> int

mss/base.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@
55
"""
66

77
from datetime import datetime
8+
from typing import TYPE_CHECKING
89

910
from .exception import ScreenShotError
1011
from .screenshot import ScreenShot
1112
from .tools import to_png
1213

14+
if TYPE_CHECKING:
15+
from typing import Any, Callable, Iterator, List, Optional, Type # noqa
16+
17+
from .models import Monitor, Monitors # noqa
18+
1319

1420
class MSSMixin:
1521
""" This class will be overloaded by a system specific one. """
1622

17-
cls_image = ScreenShot # type: object
18-
compression_level = 6 # type: int
23+
cls_image = ScreenShot # type: Type[ScreenShot]
24+
compression_level = 6
1925

2026
def __enter__(self):
2127
# type: () -> MSSMixin
@@ -24,7 +30,6 @@ def __enter__(self):
2430
return self
2531

2632
def __exit__(self, *_):
27-
# type: (*str) -> None
2833
""" For the cool call `with MSS() as mss:`. """
2934

3035
self.close()
@@ -34,7 +39,7 @@ def close(self):
3439
""" Clean-up. """
3540

3641
def grab(self, monitor):
37-
# type: (Dict[str, int]) -> ScreenShot
42+
# type: (Monitor) -> ScreenShot
3843
"""
3944
Retrieve screen pixels for a given monitor.
4045
@@ -47,7 +52,7 @@ def grab(self, monitor):
4752

4853
@property
4954
def monitors(self):
50-
# type: () -> List[Dict[str, int]]
55+
# type: () -> Monitors
5156
"""
5257
Get positions of all monitors.
5358
If the monitor has rotation, you have to deal with it
@@ -131,6 +136,7 @@ def save(self, mon=0, output="monitor-{mon}.png", callback=None):
131136
yield output
132137

133138
def shot(self, **kwargs):
139+
# type: (Any) -> str
134140
"""
135141
Helper to save the screen shot of the 1st monitor, by default.
136142
You can pass the same arguments as for ``save``.
@@ -142,7 +148,6 @@ def shot(self, **kwargs):
142148
@staticmethod
143149
def _cfactory(attr, func, argtypes, restype, errcheck=None):
144150
# type: (Any, str, List[Any], Any, Optional[Callable]) -> None
145-
# pylint: disable=too-many-locals
146151
""" Factory to create a ctypes function and automatically manage errors. """
147152

148153
meth = getattr(attr, func)

mss/darwin.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@
1111
import ctypes
1212
import ctypes.util
1313
import sys
14+
from typing import TYPE_CHECKING
1415

1516
from .base import MSSMixin
1617
from .exception import ScreenShotError
1718
from .screenshot import Size
1819

20+
if TYPE_CHECKING:
21+
from typing import Any, List, Type, Union # noqa
22+
23+
from .models import Monitor, Monitors # noqa
24+
from .screenshot import ScreenShot # noqa
25+
1926
__all__ = ("MSS",)
2027

2128

2229
def cgfloat():
23-
# type: () -> Any
30+
# type: () -> Union[Type[ctypes.c_double], Type[ctypes.c_float]]
2431
""" Get the appropriate value for a float. """
2532

2633
return ctypes.c_double if sys.maxsize > 2 ** 32 else ctypes.c_float
@@ -61,13 +68,11 @@ class MSS(MSSMixin):
6168
It uses intensively the CoreGraphics library.
6269
"""
6370

64-
max_displays = 32 # type: int
65-
66-
def __init__(self):
67-
# type: () -> None
71+
def __init__(self, **_):
6872
""" macOS initialisations. """
6973

70-
self._monitors = [] # type: List[Dict[str, int]]
74+
self._monitors = [] # type: Monitors
75+
self.max_displays = 32
7176

7277
coregraphics = ctypes.util.find_library("CoreGraphics")
7378
if not coregraphics:
@@ -77,13 +82,15 @@ def __init__(self):
7782
self._set_cfunctions()
7883

7984
def _set_cfunctions(self):
85+
# type: () -> None
8086
""" Set all ctypes functions and attach them to attributes. """
8187

82-
def cfactory(attr=self.core, func=None, argtypes=None, restype=None):
83-
# type: (Any, str, List[Any], Any) -> None
84-
# pylint: disable=too-many-locals
88+
def cfactory(func, argtypes, restype):
89+
# type: (str, List[Any], Any) -> None
8590
""" Factorize ctypes creations. """
86-
self._cfactory(attr=attr, func=func, argtypes=argtypes, restype=restype)
91+
self._cfactory(
92+
attr=self.core, func=func, argtypes=argtypes, restype=restype
93+
)
8794

8895
uint32 = ctypes.c_uint32
8996
void = ctypes.c_void_p
@@ -117,7 +124,7 @@ def cfactory(attr=self.core, func=None, argtypes=None, restype=None):
117124

118125
@property
119126
def monitors(self):
120-
# type: () -> List[Dict[str, int]]
127+
# type: () -> Monitors
121128
""" Get positions of monitors (see parent class). """
122129

123130
if not self._monitors:
@@ -165,7 +172,7 @@ def monitors(self):
165172
return self._monitors
166173

167174
def grab(self, monitor):
168-
# type: (Dict[str, int]) -> ScreenShot
175+
# type: (Monitor) -> ScreenShot
169176
"""
170177
See :meth:`MSSMixin.grab <mss.base.MSSMixin.grab>` for full details.
171178
"""

mss/exception.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
Source: https://github.com/BoboTiG/python-mss
55
"""
66

7+
from typing import TYPE_CHECKING
8+
9+
if TYPE_CHECKING:
10+
from typing import Any, Dict # noqa
11+
712

813
class ScreenShotError(Exception):
914
""" Error handling class. """
1015

1116
def __init__(self, message, details=None):
12-
# type: (Dict[str, Any]) -> None
17+
# type: (str, Dict[str, Any]) -> None
1318
super().__init__(message)
1419
self.details = details or {}

mss/factory.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
def mss(**kwargs):
13-
# type: (**str) -> MSS
1413
""" Factory returning a proper MSS class instance.
1514
1615
It detects the plateform we are running on
@@ -25,11 +24,11 @@ def mss(**kwargs):
2524

2625
operating_system = platform.system().lower()
2726
if operating_system == "darwin":
28-
from .darwin import MSS
27+
from .darwin import MSS # type: ignore
2928
elif operating_system == "linux":
30-
from .linux import MSS
29+
from .linux import MSS # type: ignore
3130
elif operating_system == "windows":
32-
from .windows import MSS
31+
from .windows import MSS # type: ignore
3332
else:
3433
raise ScreenShotError(
3534
"System {!r} not (yet?) implemented.".format(operating_system)

0 commit comments

Comments
 (0)