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

chore: Dead dependencies and code cleanup #13344

Merged
merged 24 commits into from Jun 3, 2021

Conversation

gavindsouza
Copy link
Collaborator

@gavindsouza gavindsouza commented May 26, 2021

Changes

  • Removed future, past utils' usages (includes unicode_literals, print_function, with_metaclass, cmp)
  • Removed code blocks for if PY2 and if not PY3, and other variations of it; and other compatibility code
  • Removed six usages (includes string_types, text_type, integer_types, urllib, StringIO, etc)
  • Used context managers at multiple places to ensure files are closed after reading them
  • Removed watchdog, future, bleach-whitelist packages from requirements.txt
  • Added bleach-allowlist as replacement for bleach-whitelist in requirements.txt
  • Escaped and used raw strings to handle invalid sequence errors and deprecation warnings
  • Converted strings to translatable for certain throws (as pointed out by semgrep)
  • Style fixes (as pointed out by sider), and code simplification
  • Dropped test_translation.py which has been completely commented out for ~7 years
  • Updated Jinja2 from 2.11.3 to 3.0.1
  • Updated PyJWT from 1.7.1 to 2.0.1

Check commit messages for more details

cmp was being used from past.builtins library since it was deprecated in
PY2. It's hard to understand behaviour of their usages, so this is an
attempt to replicate behaviour with simpler logic, making this more
readable.

Also, removed usages of iteritems and string_types, compatibility
imports
* Remove six for PY2 compatability since our dependencies are not, PY2
  is legacy.
* Removed usages of utils from future/past libraries since they are
  deprecated. This includes 'from __future__ ...' and 'from past...'
  statements.
* Removed compatibility imports for PY2, switched from six imports to
  standard library imports.
* Removed utils code blocks that handle operations depending on PY2/3
  versions.
* Removed 'from __future__ ...' lines from templates/code generators
* Used PY3 syntaxes in place of PY2 compatible blocks. eg: metaclass
* The library bleach-whitelist was deprecated and renamed to
bleach-allowlist.
* Updated the usages and requirements for the same.
While executing git commands in the shell via Frappe processes, use
context managers to ensure files get closed after usage. This fixes the
ResourceWarning errors due to unclosed files.
Watchdog isn't used by Frappe, and there wasn't any mechanism to access
it directly either. By default, bench serve (or start) uses
Werkzeug's watchdogreloader
This fixes the ResourceWarning errors due to unclosed files while
utilizing the website router
@coveralls
Copy link

coveralls commented May 26, 2021

Coverage Status

Coverage decreased (-0.08%) to 52.103% when pulling 2ad9d20 on gavindsouza:drop-py2-code into 099c2f0 on frappe:develop.

@gavindsouza
Copy link
Collaborator Author

gavindsouza commented May 27, 2021

Found the following deprecation warnings in the CI tests that broke after the last merge. Will accommodate them in this PR.

/home/runner/frappe-bench/env/lib/python3.7/site-packages/jinja2/environment.py:1088: DeprecationWarning: 'soft_unicode' has been renamed to 'soft_str'. The old name will be removed in MarkupSafe 2.1.
DeprecationWarning: deprecated [__init__.py:6]
DeprecationWarning: It is strongly recommended that you pass in a value for the "algorithms" argument when calling decode(). This argument will be mandatory in a future version. [api_jws.py:145]

Edit: Updated Jinja2 and PyJWT to get rid of those.

@revant

This comment has been minimized.

@revant
Copy link
Collaborator

revant commented May 31, 2021

bench setup requirements solved previous error,

❯ bench --site test.localhost reinstall --yes --admin-password admin

Installing frappe...
Updating DocTypes for frappe        : [====                                    ] 12%Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/database/database.py", line 143, in sql
    self._cursor.execute(query)
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/pymysql/cursors.py", line 148, in execute
    result = self._query(query)
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/pymysql/cursors.py", line 310, in _query
    conn.query(q)
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 548, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 775, in _read_query_result
    result.read()
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 1156, in read
    first_packet = self.connection._read_packet()
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/pymysql/connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/pymysql/protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/pymysql/err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1146, "Table '_fcadc07dbdbc21db.tabDocument Naming Rule' doesn't exist")

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/revant/.pyenv/versions/3.7.10/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/revant/.pyenv/versions/3.7.10/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/revant/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 104, in <module>
    main()
  File "/home/revant/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 18, in main
    click.Group(commands=commands)(prog_name='bench')
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 26, in _func
    ret = f(frappe._dict(ctx.obj), *args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/commands/site.py", line 148, in reinstall
    _reinstall(site, admin_password, mariadb_root_username, mariadb_root_password, yes, verbose=context.verbose)
  File "/home/revant/frappe-bench/apps/frappe/frappe/commands/site.py", line 171, in _reinstall
    admin_password=admin_password)
  File "/home/revant/frappe-bench/apps/frappe/frappe/installer.py", line 79, in _new_site
    install_app(app, verbose=verbose, set_as_patched=not source_sql)
  File "/home/revant/frappe-bench/apps/frappe/frappe/installer.py", line 160, in install_app
    sync_for(name, force=True, sync_everything=True, verbose=verbose, reset_permissions=True)
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/sync.py", line 68, in sync_for
    reset_permissions=reset_permissions, for_sync=True)
  File "/home/revant/frappe-bench/apps/frappe/frappe/modules/import_file.py", line 67, in import_file_by_path
    ignore_version=ignore_version, reset_permissions=reset_permissions)
  File "/home/revant/frappe-bench/apps/frappe/frappe/modules/import_file.py", line 149, in import_doc
    doc.insert()
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 265, in insert
    self.run_post_save_methods()
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 991, in run_post_save_methods
    self.run_method("on_update")
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 856, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 1145, in composer
    return composed(self, method, *args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 1128, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 850, in <lambda>
    fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.py", line 300, in on_update
    make_module_and_roles(self)
  File "/home/revant/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.py", line 1214, in make_module_and_roles
    r.insert()
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 230, in insert
    self.set_new_name(set_name=set_name, set_child_names=set_child_names)
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/document.py", line 420, in set_new_name
    set_new_name(self)
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/naming.py", line 41, in set_new_name
    set_naming_from_document_naming_rule(doc)
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/naming.py", line 95, in set_naming_from_document_naming_rule
    dict(document_type=doc.doctype, disabled=0), order_by='priority desc', ignore_ddl=True):
  File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 1432, in get_all
    return get_list(doctype, *args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/__init__.py", line 1405, in get_list
    return frappe.model.db_query.DatabaseQuery(doctype).execute(*args, **kwargs)
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/db_query.py", line 101, in execute
    result = self.build_and_run()
  File "/home/revant/frappe-bench/apps/frappe/frappe/model/db_query.py", line 139, in build_and_run
    update=self.update, ignore_ddl=self.ignore_ddl)
  File "/home/revant/frappe-bench/apps/frappe/frappe/database/database.py", line 161, in sql
    if ignore_ddl and (self.is_missing_column(e) or self.is_missing_table(e) or self.cant_drop_field_or_key(e)):
AttributeError: 'MariaDBDatabase' object has no attribute 'is_missing_table'

reinstall again solved it.

I'm not able to successfully run tests locally

❯ bench --site test.localhost run-tests --module frappe.tests.test_oauth20
Updating Dashboard for frappe

test_invalid_login (frappe.tests.test_oauth20.TestOAuth20) (4.33s)
.EEFEEE
======================================================================
ERROR: test_login_using_authorization_code (frappe.tests.test_oauth20.TestOAuth20)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_oauth20.py", line 64, in test_login_using_authorization_code
    auth_code = query.get("code")[0]
TypeError: 'NoneType' object is not subscriptable

======================================================================
ERROR: test_login_using_authorization_code_with_pkce (frappe.tests.test_oauth20.TestOAuth20)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_oauth20.py", line 116, in test_login_using_authorization_code_with_pkce
    auth_code = query.get("code")[0]
TypeError: 'NoneType' object is not subscriptable

======================================================================
ERROR: test_openid_code_id_token (frappe.tests.test_oauth20.TestOAuth20)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_oauth20.py", line 282, in test_openid_code_id_token
    auth_code = query.get("code")[0]
TypeError: 'NoneType' object is not subscriptable

======================================================================
ERROR: test_resource_owner_password_credentials_grant (frappe.tests.test_oauth20.TestOAuth20)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_oauth20.py", line 216, in test_resource_owner_password_credentials_grant
    bearer_token = token_response.json()
  File "/home/revant/frappe-bench/env/lib/python3.7/site-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/home/revant/.pyenv/versions/3.7.10/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/home/revant/.pyenv/versions/3.7.10/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/home/revant/.pyenv/versions/3.7.10/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

======================================================================
ERROR: test_revoke_token (frappe.tests.test_oauth20.TestOAuth20)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_oauth20.py", line 166, in test_revoke_token
    auth_code = query.get("code")[0]
TypeError: 'NoneType' object is not subscriptable

======================================================================
FAIL: test_login_using_implicit_token (frappe.tests.test_oauth20.TestOAuth20)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_oauth20.py", line 249, in test_login_using_implicit_token
    self.assertTrue(response_dict.get("access_token"))
AssertionError: None is not true

----------------------------------------------------------------------
Ran 7 tests in 4.612s

FAILED (failures=1, errors=5)
❯ code sites/test.localhost/site_config.json
❯ bench --site test.localhost reinstall --yes --admin-password admin

Installing frappe...
Updating DocTypes for frappe        : [========================================] 100%
Updating country info               : [========================================] 100%
*** Scheduler is disabled ***
❯ bench --site test.localhost run-tests --module frappe.tests.test_api
Updating Dashboard for frappe
..FFFFFFF.F
======================================================================
FAIL: test_create_document (frappe.tests.test_api.TestResourceAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_api.py", line 122, in test_create_document
    self.assertEqual(response.status_code, 200)
AssertionError: 403 != 200

======================================================================
FAIL: test_delete_document (frappe.tests.test_api.TestResourceAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_api.py", line 143, in test_delete_document
    self.assertEqual(response.status_code, 202)
AssertionError: 403 != 202

======================================================================
FAIL: test_get_list (frappe.tests.test_api.TestResourceAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_api.py", line 79, in test_get_list
    self.assertEqual(response.status_code, 200)
AssertionError: 403 != 200

======================================================================
FAIL: test_get_list_debug (frappe.tests.test_api.TestResourceAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_api.py", line 106, in test_get_list_debug
    self.assertEqual(response.status_code, 200)
AssertionError: 403 != 200

======================================================================
FAIL: test_get_list_dict (frappe.tests.test_api.TestResourceAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_api.py", line 93, in test_get_list_dict
    self.assertEqual(response.status_code, 200)
AssertionError: 403 != 200

======================================================================
FAIL: test_get_list_fields (frappe.tests.test_api.TestResourceAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_api.py", line 114, in test_get_list_fields
    self.assertEqual(response.status_code, 200)
AssertionError: 403 != 200

======================================================================
FAIL: test_get_list_limit (frappe.tests.test_api.TestResourceAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_api.py", line 86, in test_get_list_limit
    self.assertEqual(response.status_code, 200)
AssertionError: 403 != 200

======================================================================
FAIL: test_update_document (frappe.tests.test_api.TestResourceAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/revant/frappe-bench/apps/frappe/frappe/tests/test_api.py", line 135, in test_update_document
    self.assertEqual(response.status_code, 200)
AssertionError: 403 != 200

----------------------------------------------------------------------
Ran 11 tests in 4.406s

FAILED (failures=8)

gavindsouza referenced this pull request Jun 3, 2021
…on_key (#13399)

* fix: only allow keys generated by fernet in encrypt()/decrypt()

* fix: sider and semgrep fixes
@gavindsouza gavindsouza merged commit f36513d into frappe:develop Jun 3, 2021
@revant
Copy link
Collaborator

revant commented Jun 3, 2021

I created a fresh develop bench and everything worked.

Sorry for wrong review and delayed reply.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants