Skip to content

Commit 8923307

Browse files
committed
add doc/define-basestring
1 parent cdaf0cf commit 8923307

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

README.MD

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,25 @@
99

1010
This project has been inspired by `'strip-hints'` and `'py-backwards'` but it is
1111
now based on the Python 3.9+ standard library's `'ast.unparse()'`. More specifically
12-
it is using `'ast_comments'` to keep the comments. The implementation wants to make
13-
it very easy so that you can add your own source code transformations to support
12+
it is optionally using `'ast_comments'` to keep the comments. The implementation wants to
13+
make it very easy so that you can add your own source code transformations to support
1414
compatibility with older python versions.
1515

1616
The `'strip-python3'` tool can extract typehints to `'*.pyi'` file containing only
1717
the outer object-oriented interface. That allows you to ship modern typed python3
1818
code to older systems, possibly even having only python2 preinstalled. The extra
19-
pyi however will allow to run it on older systems with a python3.6 installed.
19+
pyi however will allow to run it on older systems with a python3.6+ installed.
2020

2121
The default configuration is to strip the `*.py` file to python2 syntax that can
2222
also work in python3 (using `__print_function__`), and (when using `-2` or `-3`)
2323
to put a `*.pyi` file next to it containing the typehints in a syntax compatible
24-
with python3.6. When not using `-2` or `-3` it just prints the type-stripped and
25-
backward-transformed python script to stdout.
24+
with python3. When not using `-2` or `-3` it just prints the type-stripped and
25+
backward-transformed python script to stdout.
26+
27+
The transformed script has not only typhints removed but a lot of library calls get replaced by python2 equivalents. Using if-def in the import section, the same code
28+
runs under python2 and python3. Optionally you can define a later python version
29+
(probably for python3.6) which reduced the number of if-def parts to ensure
30+
compatibility across python versions.
2631

2732
# references
2833

@@ -131,7 +136,7 @@ if python2 compatibility is requested.
131136

132137
If `isinstance(x, str)` is used then each is replaced by `isinstance(x, basestring)`
133138
if python2 compatibility is requested. It does also import `basestring = str` for
134-
python3 versions.
139+
python3 versions. (see [doc](doc/define-basestring.md))
135140

136141
## define-range
137142

doc/define-basestring.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## define-basestring
2+
3+
The define-basestring transformation came from the habit that python2
4+
uses "str" as a kind of "bytes"-variant. In order to support 2to3 the
5+
python2 interpreter contains a "basestring" definition. I dont care
6+
if that is a real base class for the two types or if it is just
7+
a union-type or prototype decleration as you only ever use it in
8+
isinstance() calls - you do never create an instance of "basestring".
9+
10+
Since the "basestring" type is missing pin ython3, the transformer will
11+
check every isinstance() call in the script. If there is a check for "str"
12+
then it gets replaced by "basestring" and the import-block gets an
13+
equals-definition for "basestring" is-a "str".
14+
15+
16+
# original
17+
def foo(x: Optional[str]):
18+
if isinstance(x, str):
19+
print(x)
20+
21+
# transformed
22+
import sys
23+
if sys.version_info >= (3, 0):
24+
basestring = str
25+
26+
def foo(x: Optional[str]):
27+
if isinstance(x, basestring):
28+
print(x)
29+
30+

0 commit comments

Comments
 (0)