Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

use python 3.6 typing #33

Merged
merged 10 commits into from
Jan 12, 2020
Merged

use python 3.6 typing #33

merged 10 commits into from
Jan 12, 2020

Conversation

orcutt989
Copy link
Contributor

@orcutt989 orcutt989 commented Jan 10, 2020

all variables and functions are typed in order to help with refactoring
fix #31

Description

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

#31

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

all variables and functions are typed in order to help with refactoring
fix mre#31
@mre
Copy link
Owner

mre commented Jan 10, 2020

Amazing! That was fast. 🚀
Looks like flake8 doesn't like our type hints. Maybe there's an easy fix?

@mre mre closed this Jan 10, 2020
@mre mre reopened this Jan 10, 2020
timelapse/__main__.py Outdated Show resolved Hide resolved
timelapse/encoder.py Outdated Show resolved Hide resolved
timelapse/__main__.py Outdated Show resolved Hide resolved
timelapse/notify.py Outdated Show resolved Hide resolved
"""
Now that we have screenshots of the user's desktop, we will stitch them
together using `ffmpeg` to create a movie. Each image will become
a single frame in the movie.
"""
# Call ffmpeg with settings compatible with QuickTime.
# https://superuser.com/a/820137
command = ["/usr/local/bin/ffmpeg", "-y",
command: List[str]= ["/usr/local/bin/ffmpeg", "-y",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess this needs a

from typing import List

import to work properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍
db3330a

subprocess.run(
['screencapture', '-S', '-o', '-x', '-D',
str(get_screen_with_mouse_index() + 1), '-t', self.format, filename],
check=True)
self.screenshot_counter += 1
check: bool =True)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a parameter that we pass to subprocess.run. check=True is equivalent to writing True in this case. We just use a named parameter here because it's more readable for 👨 👩. Guess we have to remove this type hint to make it syntactically correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍 bb76c7c

@orcutt989
Copy link
Contributor Author

orcutt989 and others added 4 commits January 10, 2020 18:25
Type hints are only allowed when a new variable is defined.
Type hints are only allowed when a new variable is defined.
@mre
Copy link
Owner

mre commented Jan 12, 2020

Skipped some more type hints for reassignments.
Type hints are only allowed on when a new variable gets introduced. 😉
Think this PR is ready to get merged! 🎉
Thanks for pushing this forward @orcutt989. timelapse has types thanks to you!

@mre mre merged commit 289d1f3 into mre:master Jan 12, 2020
@orcutt989
Copy link
Contributor Author

Wooo! Thanks for letting me help!

@estysdesu
Copy link
Contributor

estysdesu commented Mar 13, 2020

Does it make sense at this point to add mypy to the dev-dependencies now for type checking? Or is there some other way these are being checked?

Mypy would greatly help catch things like how a float type was passed to a function argument that is typed as int. It probably should be typed as Union[int, float] or something numeric like such. Although not big for this case as Python can handle that without hitting an exception, mypy also helped detect when passing an Exception type to an argument thats expecting a str which could be a bigger issue (not in this case though).

EDIT: mypy reports multiple typing errors (omitting the library stubs missing as that was expected and can be configured off):

(timelapse-aCV8DQCs-py3.7) bash-5.0$ mv timelapse/__init.py__ timelapse/__init__.py    # see issue #43 
(timelapse-aCV8DQCs-py3.7) bash-5.0$ mypy -p timelapse
timelapse/__main__.py:9: error: Cannot find implementation or library stub for module named 'notify'    # imports should be renamed for packages to import timelapse.notify or from . import notify (https://docs.python.org/3.7/tutorial/modules.html#intra-package-references)
timelapse/__main__.py:10: error: Cannot find implementation or library stub for module named 'encoder'    # see above
timelapse/__main__.py:11: error: Cannot find implementation or library stub for module named 'recorder'    # see above
timelapse/__main__.py:47: error: Name 'NSObject' is not defined    # to satisfy mypy, all of these objects should be explicitly imported (from AppKit import NSObject, NSStatusBar, ...) or use # type: ignore
timelapse/__main__.py:63: error: Name 'NSStatusBar' is not defined
...
timelapse/__main__.py:99: error: Name 'NSMenuItem' is not defined
timelapse/__main__.py:103: error: Name 'NSMenuItem' is not defined
timelapse/__main__.py:110: error: "None" has no attribute "join"    # typing is not present for self.recorder; mypy infered incorrect type because it was set to None; type should be Optional[Recorder]
timelapse/__main__.py:121: error: Attribute 'recording' already defined on line 54    # type defined twice
Found 24 errors in 3 files (checked 5 source files)

I'd be happy to submit a PR that

  1. Adds mypy to the dev dependencies
  2. Cleans up mypy issues

Just let me know if this is something you'd be interested in. Glad to see this project make use of Python typing. I think its really valuable.

@mre mre mentioned this pull request Mar 13, 2020
10 tasks
@mre
Copy link
Owner

mre commented Mar 13, 2020

Thanks for looking into this @estysdesu.
A PR is most welcome.
As a start, I've added a Python linter to the CI build process in #45,
but we should still add mypy to the dev dependencies and clean up the issues. 👍

@mre
Copy link
Owner

mre commented Mar 13, 2020

Your code has been rated at 1.47/10

There's a bit of work to do, I guess. 😆

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use Python 3 types
3 participants