pospell should not raise exception if invalid/missing dictionary is passed #4

Closed
opened 2023-04-10 11:38:22 +00:00 by jose1711 · 8 comments

Currently a TypeError is raised if a dictionary passed is not found.

$ pospell -l missing foo.po
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Traceback (most recent call last):
  File "/usr/bin/pospell", line 33, in <module>
    sys.exit(load_entry_point('pospell==1.1', 'console_scripts', 'pospell')())
  File "/usr/lib/python3.10/site-packages/pospell.py", line 495, in main
    errors = spell_check(
  File "/usr/lib/python3.10/site-packages/pospell.py", line 399, in spell_check
    errors = flatten(
  File "/usr/lib/python3.10/site-packages/pospell.py", line 357, in flatten
    return [element for a_list in list_of_lists for element in a_list]
  File "/usr/lib/python3.10/site-packages/pospell.py", line 357, in <listcomp>
    return [element for a_list in list_of_lists for element in a_list]
TypeError: 'int' object is not iterable

This should be handled more gracefully.

Currently a `TypeError` is raised if a dictionary passed is not found. ``` $ pospell -l missing foo.po Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Traceback (most recent call last): File "/usr/bin/pospell", line 33, in <module> sys.exit(load_entry_point('pospell==1.1', 'console_scripts', 'pospell')()) File "/usr/lib/python3.10/site-packages/pospell.py", line 495, in main errors = spell_check( File "/usr/lib/python3.10/site-packages/pospell.py", line 399, in spell_check errors = flatten( File "/usr/lib/python3.10/site-packages/pospell.py", line 357, in flatten return [element for a_list in list_of_lists for element in a_list] File "/usr/lib/python3.10/site-packages/pospell.py", line 357, in <listcomp> return [element for a_list in list_of_lists for element in a_list] TypeError: 'int' object is not iterable ``` This should be handled more gracefully.
Owner

Can you try the mdk-missing-dict branch, typically via:

python3 -m pip install --force-reinstall git+https://git.afpy.org/AFPy/pospell@mdk-missing-dict
Can you try the mdk-missing-dict branch, typically via: python3 -m pip install --force-reinstall git+https://git.afpy.org/AFPy/pospell@mdk-missing-dict
Author

Not working well.

Traceback (most recent call last):
  File "/home/test/.local/bin/pospell", line 8, in <module>
    sys.exit(main())
  File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 475, in main
    args = parse_args()
  File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 316, in parse_args
    if not args.personal_dict.exists():
AttributeError: 'NoneType' object has no attribute 'exists'
Not working well. ``` Traceback (most recent call last): File "/home/test/.local/bin/pospell", line 8, in <module> sys.exit(main()) File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 475, in main args = parse_args() File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 316, in parse_args if not args.personal_dict.exists(): AttributeError: 'NoneType' object has no attribute 'exists' ```
Owner

Haha :(

I fixed the fix, can you please try again?

Haha :( I fixed the fix, can you please try again?
Author

Different error now.

$ .local/bin/pospell -l missing foo.po
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Traceback (most recent call last):
  File "/home/test/.local/bin/pospell", line 8, in <module>
    sys.exit(main())
  File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 500, in main
    errors = spell_check(
  File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 404, in spell_check
    errors = flatten(
  File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 362, in flatten
    return [element for a_list in list_of_lists for element in a_list]
  File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 362, in <listcomp>
    return [element for a_list in list_of_lists for element in a_list]
TypeError: 'int' object is not iterable

This is what's inside list_of_lists raising a TypeError in return [element for a_list in list_of_lists for element in a_list]:

(Pdb) list_of_lists
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
Different error now. ``` $ .local/bin/pospell -l missing foo.po Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Traceback (most recent call last): File "/home/test/.local/bin/pospell", line 8, in <module> sys.exit(main()) File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 500, in main errors = spell_check( File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 404, in spell_check errors = flatten( File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 362, in flatten return [element for a_list in list_of_lists for element in a_list] File "/home/test/.local/lib/python3.10/site-packages/pospell.py", line 362, in <listcomp> return [element for a_list in list_of_lists for element in a_list] TypeError: 'int' object is not iterable ``` This is what's inside `list_of_lists` raising a TypeError in `return [element for a_list in list_of_lists for element in a_list]`: ``` (Pdb) list_of_lists [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1] ```
Owner

Ohhh nice catch. It happened because you mixed the -l and -d argument, but yet it should not crash.

I fixed it.

Can you try again?

Ohhh nice catch. It happened because you mixed the `-l` and `-d` argument, but yet it should not crash. I fixed it. Can you try again?
Contributor

I followed the OP command in the latest and it still gives TypeError on invalid value for -l in the latest commit cf6c1c8.

# pospell -l missing about.po 
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Can't open affix or dictionary files for dictionary named "missing".
Traceback (most recent call last):
  File "/home/rffontenelle/Projects/pospell/venv/bin/pospell", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/rffontenelle/Projects/pospell/pospell.py", line 497, in main
    errors = spell_check(
             ^^^^^^^^^^^^
  File "/home/rffontenelle/Projects/pospell/pospell.py", line 401, in spell_check
    errors = flatten(
             ^^^^^^^^
  File "/home/rffontenelle/Projects/pospell/pospell.py", line 359, in flatten
    return [element for a_list in list_of_lists for element in a_list]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rffontenelle/Projects/pospell/pospell.py", line 359, in <listcomp>
    return [element for a_list in list_of_lists for element in a_list]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Type`Error: 'int' object is not iterable
I followed the OP command in the latest and it still gives TypeError on invalid value for `-l` in the latest commit `cf6c1c8`. ``` # pospell -l missing about.po Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Can't open affix or dictionary files for dictionary named "missing". Traceback (most recent call last): File "/home/rffontenelle/Projects/pospell/venv/bin/pospell", line 8, in <module> sys.exit(main()) ^^^^^^ File "/home/rffontenelle/Projects/pospell/pospell.py", line 497, in main errors = spell_check( ^^^^^^^^^^^^ File "/home/rffontenelle/Projects/pospell/pospell.py", line 401, in spell_check errors = flatten( ^^^^^^^^ File "/home/rffontenelle/Projects/pospell/pospell.py", line 359, in flatten return [element for a_list in list_of_lists for element in a_list] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/rffontenelle/Projects/pospell/pospell.py", line 359, in <listcomp> return [element for a_list in list_of_lists for element in a_list] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type`Error: 'int' object is not iterable ```
Contributor

So, it seems the integer objects are caused by parcial = functools.partial(run_hunspell, language, personal_dict) (line 386) returning a list with [-1, -1, -1, -1] because the function run_hunspell returns -1 (line 336) when subprocess.CalledProcessError is raised (and it raises because hunspell doesn't find the language missing).

So, it seems the integer objects are caused by `parcial = functools.partial(run_hunspell, language, personal_dict)` ([line 386](https://git.afpy.org/AFPy/pospell/src/commit/8b753bde26fe612ba397a4b4aafa7a0560402805/pospell.py#L386)) returning a list with `[-1, -1, -1, -1]` because the function `run_hunspell` returns `-1` ([line 336](https://git.afpy.org/AFPy/pospell/src/commit/8b753bde26fe612ba397a4b4aafa7a0560402805/pospell.py#L336)) when `subprocess.CalledProcessError` is raised (and it raises because hunspell doesn't find the language `missing`).
Owner

I think this is fixed. I added type annotation to run_hunspell to ensure we don't return -1 in the future.

I think this is fixed. I added type annotation to `run_hunspell` to ensure we don't return -1 in the future.
mdk closed this issue 2023-11-20 11:03:52 +00:00
Sign in to join this conversation.
No Label
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: AFPy/pospell#4
No description provided.