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

Add type hints #1828

Draft
wants to merge 49 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
05c7fbb
Add type hints for BaseClient.__init__
Harmon758 Feb 26, 2022
b119e72
Remove redundant signature and type documentation for Client
Harmon758 Feb 26, 2022
f208c8c
Add type hints for Client methods
Harmon758 Feb 26, 2022
cdd406e
Remove redundant type documentation for Client methods
Harmon758 Feb 26, 2022
bb777f8
Merge branch 'master' into type-hints
Harmon758 Feb 26, 2022
be295b4
Add typing-extensions requirement
Harmon758 Feb 26, 2022
cbb8fa6
Add type hints for parse_datetime
Harmon758 Feb 27, 2022
89632a6
Add type hints for list_to_csv
Harmon758 Feb 27, 2022
0d07d5c
Add type hints for **params in Client methods
Harmon758 Feb 27, 2022
094079f
Add and use JSON TypeAlias for Client method return type hints
Harmon758 Feb 27, 2022
781b95b
Check TYPE_CHECKING for type alias definitions in client.py
Harmon758 Feb 27, 2022
9dbbbfc
Use JSON type alias in Client methods
Harmon758 Feb 27, 2022
ef6e930
Merge branch 'master' into type-hints
Harmon758 Feb 27, 2022
ec193c9
add: mypy to CI
eggplants Mar 8, 2022
20280d5
add: mypy configuration
eggplants Mar 8, 2022
daaed00
add: stub libraries
eggplants Mar 8, 2022
36bcf3f
add: bit more type annotations
eggplants Mar 8, 2022
b75d923
add: import statement for Dict
eggplants Mar 8, 2022
f5f5a69
fix: version specifier in install_requires
eggplants Mar 8, 2022
28b8da7
add: mypy to test deps
eggplants Mar 8, 2022
ba7593d
add: throw TypeError when access_token is not set in client.py
eggplants Mar 8, 2022
6c71fbc
Merge branch 'master' into type-hints
Harmon758 Mar 24, 2022
f514266
Merge branch 'type-hints' into add_bit_type_hint
eggplants Mar 24, 2022
fc503ef
Ignore mypy error
eggplants Mar 24, 2022
02781d1
Add collections.abc.Iterable import when type checking in client.py
Harmon758 Apr 4, 2022
e5fe8f1
Only import typing.Any when type checking in client.py
Harmon758 Apr 4, 2022
6260246
Merge branch 'type-hints' into add_bit_type_hint
Harmon758 Apr 4, 2022
554a83b
Update .github/workflows/test.yml
eggplants Apr 4, 2022
ceb5ef3
Update .github/workflows/test.yml
eggplants Apr 4, 2022
ed152b3
Update tweepy/auth.py
eggplants Apr 4, 2022
ca7f554
Move typing-extensions requirement to new types extra
Harmon758 Apr 5, 2022
ed58b61
fix: use __future__.annotations
eggplants Apr 5, 2022
d4892ab
Merge branch 'type-hints' into add_bit_type_hint
eggplants Apr 5, 2022
39a2535
add: install types-extra in ci
eggplants Apr 5, 2022
853ba19
Improve order of requirements for types extra
Harmon758 Apr 5, 2022
5b91003
Improve __future__ import formatting in cache.py
Harmon758 Apr 5, 2022
ba5cb5c
Improve __future__ import formatting in streaming.py
Harmon758 Apr 5, 2022
100770c
Merge pull request #1840 from eggplants/add_bit_type_hint
Harmon758 Apr 5, 2022
47cdc2a
Merge branch 'master' into type-hints
Harmon758 Apr 5, 2022
0d502d3
Add type hints for Client.get_quote_tweets
Harmon758 Apr 5, 2022
647df26
Add type hints for Client bookmarks methods
Harmon758 Apr 5, 2022
c8fc376
Merge branch 'master' into type-hints
Harmon758 Apr 6, 2022
063529b
Merge branch 'master' into type-hints
Harmon758 May 20, 2022
6366d01
Ignore async_lru library missing type stubs
Harmon758 May 20, 2022
52e0a38
Ignore type error for handled functools.cache ImportError
Harmon758 May 20, 2022
5c11e9d
Merge branch 'master' into type-hints
Harmon758 May 21, 2022
71f4e4b
Add type hints for Client.get_home_timeline
Harmon758 May 21, 2022
0a6710f
Merge branch 'master' into type-hints
Harmon758 Oct 28, 2022
045a168
Add type hints for Client Direct Message methods
Harmon758 Oct 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[async,dev,test]
pip install .[async,dev,test,types]
- name: Type-check
run: |
mypy tweepy
- name: Run tests
run: |
coverage run -m unittest discover tests
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ To use the `tweepy.asynchronous` subpackage, be sure to install with the

pip install tweepy[async]

For type hint dependencies, install with the `types` extra.

You can also use Git to clone the repository from GitHub to install the latest
development version:

Expand Down
3 changes: 3 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
'sphinx_tabs.tabs'
]

autodoc_typehints = 'description'
autodoc_type_aliases = {'JSON': 'tweepy.client.JSON'}

hoverxref_auto_ref = True
hoverxref_domains = ['py']
hoverxref_intersphinx = ['aiohttp', 'requests', 'requests_oauthlib']
Expand Down
2 changes: 2 additions & 0 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ To use the ``tweepy.asynchronous`` subpackage, be sure to install with the

pip install tweepy[async]

For type hint dependencies, install with the ``types`` extra.

You can also use Git to clone the repository from GitHub to install the latest
development version::

Expand Down
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ omit =
*/python?.?/*
*/site-packages/*

[mypy]
show_error_codes = True
pretty = True
# strict = True

[tox:tox]
envlist = py37, py38, py39, py310, py311

Expand Down
10 changes: 9 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@
"tox>=3.21.0",
],
"socks": ["requests[socks]>=2.27.0,<3"],
"test": ["vcrpy>=1.10.3"],
"test": [
"mypy>=0.931",
"vcrpy>=1.10.3",
],
"types": [
"types-oauthlib>=3.1.6,<4",
"types-requests>=2.27.11,<3",
"typing-extensions>=4.0,<5",
],
},
test_suite="tests",
keywords="twitter library",
Expand Down
2 changes: 1 addition & 1 deletion tweepy/asynchronous/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

try:
import aiohttp
import async_lru
import async_lru # type: ignore[import]
import oauthlib
except ModuleNotFoundError:
from tweepy.errors import TweepyException
Expand Down
4 changes: 2 additions & 2 deletions tweepy/asynchronous/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# See LICENSE for details.

try:
from functools import cache
from functools import cache # type: ignore[attr-defined]
except ImportError: # Remove when support for Python 3.8 is dropped
from functools import lru_cache
cache = lru_cache(maxsize=None)
Expand All @@ -14,7 +14,7 @@
import time

import aiohttp
from async_lru import alru_cache
from async_lru import alru_cache # type: ignore[import]
from oauthlib.oauth1 import Client as OAuthClient
from yarl import URL

Expand Down
4 changes: 3 additions & 1 deletion tweepy/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import requests
from requests.auth import AuthBase, HTTPBasicAuth
from requests_oauthlib import OAuth1, OAuth1Session, OAuth2Session
from requests_oauthlib import ( # type: ignore[import]
OAuth1, OAuth1Session, OAuth2Session
)

from tweepy.errors import TweepyException

Expand Down
6 changes: 4 additions & 2 deletions tweepy/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Copyright 2009-2022 Joshua Roesslein
# See LICENSE for details.

from __future__ import annotations

import datetime
import hashlib
import logging
Expand Down Expand Up @@ -129,7 +131,7 @@ class FileCache(Cache):
"""File-based cache"""

# locks used to make cache thread-safe
cache_locks = {}
cache_locks: dict[str, threading.Lock] = {}

def __init__(self, cache_dir, timeout=60):
Cache.__init__(self, timeout)
Expand Down Expand Up @@ -398,7 +400,7 @@ def __init__(self, db, timeout=3600, collection='tweepy_cache'):
self.col.create_index('created', expireAfterSeconds=timeout)

def store(self, key, value):
from bson.binary import Binary
from bson.binary import Binary # type: ignore[import]

now = datetime.datetime.utcnow()
blob = Binary(pickle.dumps(value))
Expand Down