browniebroke / django-codemod

A tool to automatically fix Django deprecations.

Home Page:https://django-codemod.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Avoid try: .. except ImportError: blocks

jayvdb opened this issue · comments

The following occurs when running on https://github.com/stephenmcd/django-forms-builder with various deprecation/removed args.

Several problems can be seen here, but IMO the main one is that both branches should just be ignored unless the parser properly understands what ImportError exceptions will occur in each branch, and avoid changes when they are supposed to trip an exception and are handled correctly in the exception block.

git diff forms_builder/example_project/urls.py
diff --git a/forms_builder/example_project/urls.py b/forms_builder/example_project/urls.py
index 78627dd..a944703 100644
--- a/forms_builder/example_project/urls.py
+++ b/forms_builder/example_project/urls.py
@@ -1,10 +1,10 @@
 from __future__ import unicode_literals
 
 try:
-    from django.urls import re_path, include
+    from django.urls import re_path as re_path, re_path, include
 except ImportError:
     # For Django 1.8 compatibility
-    from django.conf.urls import url as re_path, include
+    from django.conf.urls import include
 from django.contrib import admin
 from django.shortcuts import render
 

When I started this tool, I had in mind Django projects, not re-usable Django app. Django app usually need to target more than one version of Django, and will have these sort of imports.

This is indeed not supported yet, we should at least put up a warning or disclaimer somewhere until we handle it better.

I think it should be feasible by disabling the visitors temporarily using the context when inside of try/except blocks

I've added a paragraph in the readme to make this limitation clearer.