Skip to content

Commit 5c916fe

Browse files
authored
Merge pull request #892 from plotly/py23
⚗️ try with future collections:
2 parents d1dc568 + 12f631a commit 5c916fe

File tree

7 files changed

+97
-95
lines changed

7 files changed

+97
-95
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ As of Dash 1.2, the renderer bundle and its peer dependencies can be packed and
4242

4343
When a change in renderer code doesn't reflect in your browser as expected, this could be: confused bundle generation, caching issue in a browser, python package not in `editable` mode, etc. The new tool reduces the risk of bundle assets by adding the digest to help compare asset changes.
4444

45+
## Python 2 And 3 Compatibility
46+
47+
Writing Python 2/3 compatible code might be a challenging task for contributors used to working on one particular version, especially new learners who start directly with Python 3.
48+
49+
From the #892, we started to adopt `python-future` instead of `six` as our tool to better achieve the goal where we can mainly write Python 3 code and make it back-compatible in Python 2.7 (last Python 2 version Dash supports before it gets deprecated).
50+
51+
Please refer to [this list of idioms](https://python-future.org/compatible_idioms.html "https://python-future.org/compatible_idioms.html") for more details on working with `python-future`.
52+
4553
## Git
4654

4755
Use the [GitHub flow](https://guides.github.com/introduction/flow/) when proposing contributions to this repository (i.e. create a feature branch and submit a PR against the default branch).

dash/_utils.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
import logging
1010
from io import open # pylint: disable=redefined-builtin
1111
from functools import wraps
12-
13-
import six
12+
import future.utils as utils
1413

1514
logger = logging.getLogger()
1615

@@ -57,9 +56,7 @@ def get_asset_path(requests_pathname, asset_path, asset_url_path):
5756

5857
# pylint: disable=no-member
5958
def patch_collections_abc(member):
60-
if six.PY2:
61-
return getattr(collections, member)
62-
return getattr(collections.abc, member)
59+
return getattr(collections if utils.PY2 else collections.abc, member)
6360

6461

6562
class AttributeDict(dict):
@@ -145,8 +142,8 @@ def run_command_with_process(cmd):
145142

146143

147144
def compute_md5(path):
148-
with open(path, encoding='utf-8') as fp:
149-
return hashlib.md5(fp.read().encode('utf-8')).hexdigest()
145+
with open(path, encoding="utf-8") as fp:
146+
return hashlib.md5(fp.read().encode("utf-8")).hexdigest()
150147

151148

152149
def job(msg=""):
@@ -157,5 +154,7 @@ def _wrapper(*args, **kwargs):
157154
res = func(*args, **kwargs)
158155
logger.info("::: 🍻🍻🍻 [%s] job done 🍻🍻🍻 :::", func.__name__)
159156
return res
157+
160158
return _wrapper
159+
161160
return wrapper

dash/dash.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from textwrap import dedent
1818

1919
import flask
20-
from flask import Flask, Response
2120
from flask_compress import Compress
2221
from werkzeug.debug.tbtools import get_current_traceback
2322

@@ -241,13 +240,13 @@ def __init__(
241240

242241
# We have 3 cases: server is either True (we create the server), False
243242
# (defer server creation) or a Flask app instance (we use their server)
244-
if isinstance(server, Flask):
243+
if isinstance(server, flask.Flask):
245244
self.server = server
246245
if name is None:
247246
name = getattr(server, 'name', '__main__')
248247
elif isinstance(server, bool):
249248
name = name if name else '__main__'
250-
self.server = Flask(name) if server else None
249+
self.server = flask.Flask(name) if server else None
251250
else:
252251
raise ValueError('server must be a Flask app or a boolean')
253252

@@ -677,7 +676,7 @@ def serve_component_suites(self, package_name, path_in_package_dist):
677676
'map': 'application/json'
678677
})[path_in_package_dist.split('.')[-1]]
679678

680-
return Response(
679+
return flask.Response(
681680
pkgutil.get_data(package_name, path_in_package_dist),
682681
mimetype=mimetype
683682
)

0 commit comments

Comments
 (0)