Repository: pallets/werkzeug

Issue:
ast.Module signature change in Python 3.8.0a3 causes TypeError
With Python 3.8.0a3 and Werkzeug 0.15.4, compiling the rule builders causes `TypeError: required field "type_ignores" missing from Module`. It sounds like `ast.Module` has a new required argument.

I think this is the same problem as https://github.com/beetbox/beets/issues/3201#issuecomment-478341869

```
flask run
 * Serving Flask app "flask_test.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
  File "d:\tool\python\38\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "d:\tool\python\38\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "d:\tool\python\38\Scripts\flask.exe\__main__.py", line 9, in <module>
  File "d:\tool\python\38\lib\site-packages\flask\cli.py", line 906, in main
    cli.main(args=args, prog_name=name)
  File "d:\tool\python\38\lib\site-packages\flask\cli.py", line 569, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "d:\tool\python\38\lib\site-packages\click\core.py", line 717, in main
    rv = self.invoke(ctx)
  File "d:\tool\python\38\lib\site-packages\click\core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "d:\tool\python\38\lib\site-packages\click\core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "d:\tool\python\38\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "d:\tool\python\38\lib\site-packages\click\decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "d:\tool\python\38\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "d:\tool\python\38\lib\site-packages\flask\cli.py", line 779, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "d:\tool\python\38\lib\site-packages\flask\cli.py", line 300, in __init__
    self._load_unlocked()
  File "d:\tool\python\38\lib\site-packages\flask\cli.py", line 324, in _load_unlocked
    self._app = rv = self.loader()
  File "d:\tool\python\38\lib\site-packages\flask\cli.py", line 381, in load_app
    app = locate_app(self, import_name, name)
  File "d:\tool\python\38\lib\site-packages\flask\cli.py", line 236, in locate_app
    __import__(module_name)
  File "D:\flask_test.py", line 3, in <module>
    app = Flask(__name__)
  File "d:\tool\python\38\lib\site-packages\flask\app.py", line 559, in __init__
    self.add_url_rule(
  File "d:\tool\python\38\lib\site-packages\flask\app.py", line 67, in wrapper_func
    return f(self, *args, **kwargs)
  File "d:\tool\python\38\lib\site-packages\flask\app.py", line 1217, in add_url_rule
    self.url_map.add(rule)
  File "d:\tool\python\38\lib\site-packages\werkzeug\routing.py", line 1388, in add
    rule.bind(self)
  File "d:\tool\python\38\lib\site-packages\werkzeug\routing.py", line 730, in bind
    self.compile()
  File "d:\tool\python\38\lib\site-packages\werkzeug\routing.py", line 794, in compile
    self._build = self._compile_builder(False).__get__(self, None)
  File "d:\tool\python\38\lib\site-packages\werkzeug\routing.py", line 951, in _compile_builder
    code = compile(module, "<werkzeug routing>", "exec")
TypeError: required field "type_ignores" missing from Module
```



Discussion:
This needs to be reported to CPython as an undocumented and backwards-incompatible API change in the standard library.
@asottile What are your thoughts? I'm not sure we should fix this yet, Python 3.8 has been causing then fixing random issues for quite a while.
Looks like this was reported in CPython https://bugs.python.org/issue35894 and the resolution was that downstream code should add a `if sys.version_info >= (3, 8)`. :unamused: 
😡 what a wacky resolution -- I'll make a more portable fix for this, coming right up!

Resolving patch:
diff --git a/src/werkzeug/routing.py b/src/werkzeug/routing.py
--- a/src/werkzeug/routing.py
+++ b/src/werkzeug/routing.py
@@ -947,7 +947,11 @@ def _join(parts):
             func_ast.args.defaults.append(ast.Str(""))
         func_ast.body = body
 
-        module = ast.fix_missing_locations(ast.Module([func_ast]))
+        # use `ast.parse` instead of `ast.Module` for better portability
+        # python3.8 changes the signature of `ast.Module`
+        module = ast.parse("")
+        module.body = [func_ast]
+        module = ast.fix_missing_locations(module)
         code = compile(module, "<werkzeug routing>", "exec")
         return self._get_func_code(code, func_ast.name)
 


Test patch: