Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Fix for cyclic import problems (issue #2935) (#2938)
Browse files Browse the repository at this point in the history
* Move domain_languages.common to semparse.common

* Move exception classes from domain_langauge.py to semparse/common/errors.py

* Undo over-specific semparse.common.error import changes

* Fix formatting issues

* Copy over common docs to .semparse.common.rst

* Fix (at least part of) the docs

* Fixing docs again

* Moving tests to the right place
  • Loading branch information
nilesh-c authored and matt-gardner committed Jun 12, 2019
1 parent 5e3c4cd commit 92ee421
Show file tree
Hide file tree
Showing 20 changed files with 67 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from allennlp.semparse.contexts import TableQuestionKnowledgeGraph
from allennlp.semparse.type_declarations import wikitables_lambda_dcs as wt_types
from allennlp.semparse.worlds import WikiTablesWorld
from allennlp.semparse.worlds.world import ParsingError
from allennlp.semparse.common.errors import ParsingError

logger = logging.getLogger(__name__) # pylint: disable=invalid-name

Expand Down
4 changes: 2 additions & 2 deletions allennlp/semparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
# dependency issues. If you want to import semparse stuff from the data code, just use a more
# complete path, like `from allennlp.semparse.worlds import WikiTablesWorld`.
from allennlp.data.tokenizers import Token as _
from allennlp.semparse.domain_languages.domain_language import (DomainLanguage, ParsingError,
ExecutionError,
from allennlp.semparse.common.errors import ParsingError, ExecutionError
from allennlp.semparse.domain_languages.domain_language import (DomainLanguage,
predicate, predicate_with_side_args)
from allennlp.semparse.worlds.world import World
from allennlp.semparse.action_space_walker import ActionSpaceWalker
1 change: 1 addition & 0 deletions allennlp/semparse/common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from allennlp.semparse.common.date import Date
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from allennlp.semparse.domain_languages.domain_language import ExecutionError
from allennlp.semparse.common.errors import ExecutionError


class Date:
Expand Down
27 changes: 27 additions & 0 deletions allennlp/semparse/common/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ParsingError(Exception):
"""
This exception gets raised when there is a parsing error during logical form processing. This
might happen because you're not handling the full set of possible logical forms, for instance,
and having this error provides a consistent way to catch those errors and log how frequently
this occurs.
"""
def __init__(self, message):
super().__init__()
self.message = message

def __str__(self):
return repr(self.message)


class ExecutionError(Exception):
"""
This exception gets raised when you're trying to execute a logical form that your executor does
not understand. This may be because your logical form contains a function with an invalid name
or a set of arguments whose types do not match those that the function expects.
"""
def __init__(self, message):
super().__init__()
self.message = message

def __str__(self):
return repr(self.message)
2 changes: 1 addition & 1 deletion allennlp/semparse/contexts/table_question_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from unidecode import unidecode
from allennlp.data.tokenizers import Token
from allennlp.semparse.domain_languages.common import Date
from allennlp.semparse.common import Date
from allennlp.semparse.contexts.knowledge_graph import KnowledgeGraph

# == stop words that will be omitted by ContextGenerator
Expand Down
4 changes: 2 additions & 2 deletions allennlp/semparse/domain_languages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from allennlp.semparse.domain_languages.domain_language import (DomainLanguage, ParsingError,
ExecutionError, START_SYMBOL,
from allennlp.semparse.domain_languages.domain_language import (DomainLanguage, START_SYMBOL,
predicate, predicate_with_side_args)
from allennlp.semparse.common.errors import ParsingError, ExecutionError
from allennlp.semparse.domain_languages.nlvr_language import NlvrLanguage
from allennlp.semparse.domain_languages.quarel_language import QuaRelLanguage
from allennlp.semparse.domain_languages.wikitables_language import WikiTablesLanguage
1 change: 0 additions & 1 deletion allennlp/semparse/domain_languages/common/__init__.py

This file was deleted.

30 changes: 1 addition & 29 deletions allennlp/semparse/domain_languages/domain_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from allennlp.common.util import START_SYMBOL
from allennlp.semparse import util
from allennlp.semparse.common.errors import ParsingError, ExecutionError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -140,35 +141,6 @@ def __eq__(self, other):
return NotImplemented


class ParsingError(Exception):
"""
This exception gets raised when there is a parsing error during logical form processing. This
might happen because you're not handling the full set of possible logical forms, for instance,
and having this error provides a consistent way to catch those errors and log how frequently
this occurs.
"""
def __init__(self, message):
super().__init__()
self.message = message

def __str__(self):
return repr(self.message)


class ExecutionError(Exception):
"""
This exception gets raised when you're trying to execute a logical form that your executor does
not understand. This may be because your logical form contains a function with an invalid name
or a set of arguments whose types do not match those that the function expects.
"""
def __init__(self, message):
super().__init__()
self.message = message

def __str__(self):
return repr(self.message)


def predicate(function: Callable) -> Callable: # pylint: disable=invalid-name
"""
This is intended to be used as a decorator when you are implementing your ``DomainLanguage``.
Expand Down
6 changes: 3 additions & 3 deletions allennlp/semparse/domain_languages/wikitables_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import logging
import re

from allennlp.semparse.domain_languages.domain_language import (DomainLanguage, ExecutionError,
PredicateType, predicate)
from allennlp.semparse.domain_languages.domain_language import (DomainLanguage, PredicateType, predicate)
from allennlp.semparse.common.errors import ExecutionError
from allennlp.semparse.contexts.table_question_knowledge_graph import MONTH_NUMBERS
from allennlp.semparse.contexts import TableQuestionContext
from allennlp.semparse.contexts.table_question_context import CellValueType
from allennlp.semparse.domain_languages.common import Date
from allennlp.semparse.common import Date
from allennlp.tools import wikitables_evaluator as evaluator

logger = logging.getLogger(__name__) # pylint: disable=invalid-name
Expand Down
3 changes: 2 additions & 1 deletion allennlp/semparse/worlds/wikitables_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from nltk.sem.logic import Type
from overrides import overrides

from allennlp.semparse.worlds.world import ParsingError, World
from allennlp.semparse.worlds.world import World
from allennlp.semparse.common.errors import ParsingError
from allennlp.semparse.type_declarations import wikitables_lambda_dcs as types
from allennlp.semparse.contexts import TableQuestionKnowledgeGraph

Expand Down
2 changes: 1 addition & 1 deletion allennlp/semparse/worlds/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from nltk.sem.logic import ApplicationExpression, Expression, LambdaExpression, BasicType, Type

from allennlp.semparse.type_declarations import type_declaration as types
from allennlp.semparse.domain_languages.domain_language import ParsingError
from allennlp.semparse.domain_languages.domain_language import nltk_tree_to_logical_form
from allennlp.semparse import util as semparse_util
from allennlp.semparse.common.errors import ParsingError

logger = logging.getLogger(__name__) # pylint: disable=invalid-name

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from allennlp.common.testing import AllenNlpTestCase
from allennlp.semparse import ExecutionError
from allennlp.semparse.domain_languages.common import Date
from allennlp.semparse.common import Date


class TestDate(AllenNlpTestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import pytest

from allennlp.common.testing import AllenNlpTestCase
from allennlp.semparse import (DomainLanguage, ExecutionError, ParsingError,
predicate, predicate_with_side_args)
from allennlp.semparse import ExecutionError, ParsingError
from allennlp.semparse import DomainLanguage, predicate, predicate_with_side_args

class Arithmetic(DomainLanguage):
def __init__(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from allennlp.data.tokenizers import Token
from allennlp.data.tokenizers import WordTokenizer
from allennlp.semparse.contexts import TableQuestionContext
from allennlp.semparse.domain_languages.domain_language import ExecutionError
from allennlp.semparse.domain_languages.common import Date
from allennlp.semparse import ExecutionError
from allennlp.semparse.common import Date
from allennlp.semparse.domain_languages.wikitables_language import WikiTablesLanguage
from allennlp.tests.semparse.domain_languages.domain_language_test import check_productions_match

Expand Down
3 changes: 2 additions & 1 deletion allennlp/tests/semparse/worlds/world_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from allennlp.common.testing import AllenNlpTestCase
from allennlp.data.tokenizers import Token
from allennlp.semparse import ParsingError, World
from allennlp.semparse import ParsingError
from allennlp.semparse import World
from allennlp.semparse.contexts import TableQuestionKnowledgeGraph
from allennlp.semparse.worlds import WikiTablesWorld

Expand Down
17 changes: 17 additions & 0 deletions doc/api/allennlp.semparse.common.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
allennlp.semparse.common
========================

.. automodule:: allennlp.semparse.common
:members:
:undoc-members:
:show-inheritance:

.. automodule:: allennlp.semparse.common.date
:members:
:undoc-members:
:show-inheritance:

.. automodule:: allennlp.semparse.common.errors
:members:
:undoc-members:
:show-inheritance:
10 changes: 0 additions & 10 deletions doc/api/allennlp.semparse.domain_languages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,3 @@ allennlp.semparse.domain_languages
:members:
:undoc-members:
:show-inheritance:

.. automodule:: allennlp.semparse.domain_languages.common
:members:
:undoc-members:
:show-inheritance:

.. automodule:: allennlp.semparse.domain_languages.common.date
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions doc/api/allennlp.semparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ allennlp.semparse

.. toctree::

allennlp.semparse.common
allennlp.semparse.contexts
allennlp.semparse.executors
allennlp.semparse.type_declarations
Expand Down

0 comments on commit 92ee421

Please sign in to comment.